Am 09.02.2014 15:52, schrieb Jiri Cincura:
> Show us the code.
>
Ok:
--- Model classes:
class CHILDDATA
{
public int CHILDID { get; set; }
public int MOTHERID { get; set; }
public string CHILDNAME { get; set; }
public virtual MOTHERDATA Mother { get; set; }
}
class MOTHERDATA
{
public int MOTHERID { get; set; }
public string MOTHERNAME { get; set; }
public virtual ICollection<CHILDDATA> Children { get; set; }
public MOTHERDATA()
{
Children = new List<CHILDDATA>();
}
}
--- Context class:
class SingleMothersDB : DbContext
{
public DbSet<MOTHERDATA> MotherRecords { get; set; }
public DbSet<CHILDDATA> ChildRecords { get; set; }
public SingleMothersDB()
: base(new FbConnection(@"database=localhost:singlemothers;
user=sib; password=immergut"), true)
{ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<MOTHERDATA>().HasKey(m => m.MOTHERID);
modelBuilder.Entity<MOTHERDATA>().Property(m => m.MOTHERNAME)
.IsRequired().HasMaxLength(50);
modelBuilder.Entity<CHILDDATA>().HasKey(c => c.CHILDID);
modelBuilder.Entity<CHILDDATA>().Property(c => c.CHILDNAME)
.IsRequired().HasMaxLength(50);
}
}
--- Application:
class Program
{
static void ShowMothersWithChildren(SingleMothersDB db)
{
Console.WriteLine("Show mothers and their children");
Console.WriteLine("Lazy loading enabled: {0}",
db.Configuration.LazyLoadingEnabled);
Console.WriteLine("Proxy creation enabled: {0}",
db.Configuration.ProxyCreationEnabled);
var motherquery = from m in db.MotherRecords
orderby m.MOTHERNAME
select m;
foreach (var m in motherquery)
{
int childcount = m.Children.Count();
Console.WriteLine("{0} (ID {1}) has {2} children:",
m.MOTHERNAME, m.MOTHERID, childcount);
foreach (var c in m.Children)
{
Console.WriteLine("{0} (ID {1})",
c.CHILDNAME, c.CHILDID);
}
Console.WriteLine();
}
}
static void ShowChildrenWithMothers(SingleMothersDB db)
{
Console.WriteLine("Show children and their mothers");
Console.WriteLine("Lazy loading enabled: {0}",
db.Configuration.LazyLoadingEnabled);
Console.WriteLine("Proxy creation enabled: {0}",
db.Configuration.ProxyCreationEnabled);
var childquery = from c in db.ChildRecords
orderby c.CHILDNAME
select c;
foreach (var c in childquery)
{
string mothername = (c.Mother == null ?
"unknown" : c.Mother.MOTHERNAME);
Console.WriteLine("{0} (Id {1}) is a child of {2}",
c.CHILDNAME, c.CHILDID, mothername);
}
Console.WriteLine();
}
static void Main(string[] args)
{
Database.SetInitializer<SingleMothersDB>(null);
using (SingleMothersDB context =
new SingleMothersDB())
{
ShowChildrenWithMothers(context);
ShowMothersWithChildren(context);
}
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
}
}
And the metadata for the database, if that's useful:
/********************* ROLES **********************/
CREATE ROLE RDB$ADMIN;
/********************* UDFS ***********************/
/****************** GENERATORS ********************/
CREATE GENERATOR S_CHILD;
CREATE GENERATOR S_MOTHER;
/******************** DOMAINS *********************/
/******************* PROCEDURES ******************/
/******************** TABLES **********************/
CREATE TABLE CHILDDATA
(
CHILDID Integer NOT NULL,
MOTHERID Integer NOT NULL,
CHILDNAME Varchar(50) NOT NULL,
PRIMARY KEY (CHILDID),
UNIQUE (CHILDNAME)
);
CREATE TABLE MOTHERDATA
(
MOTHERID Integer NOT NULL,
MOTHERNAME Varchar(50) NOT NULL,
PRIMARY KEY (MOTHERID),
UNIQUE (MOTHERNAME)
);
/********************* VIEWS **********************/
/******************* EXCEPTIONS *******************/
/******************** TRIGGERS ********************/
SET TERM ^ ;
CREATE TRIGGER SET_CHILDID FOR CHILDDATA ACTIVE
BEFORE INSERT POSITION 0
AS BEGIN
IF (NEW.childid IS NULL) THEN
NEW.childid = NEXT VALUE FOR s_child;
END^
SET TERM ; ^
SET TERM ^ ;
CREATE TRIGGER SET_MOTHERID FOR MOTHERDATA ACTIVE
BEFORE INSERT POSITION 0
AS BEGIN
IF (NEW.motherid IS NULL) THEN
NEW.motherid = NEXT VALUE FOR s_mother;
END^
SET TERM ; ^
ALTER TABLE CHILDDATA ADD
FOREIGN KEY (MOTHERID) REFERENCES MOTHERDATA (MOTHERID);
Thank you,
Sibylle
------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121051231&iu=/4140/ostg.clktrk
_______________________________________________
Firebird-net-provider mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider