如果我想由来自多个表我只是添加所有表到.edmx文件数据的对象。从那里我可以创建一个能够得到我想要的任何东西的类。没有必要进行1:1配对。在我的创建对象的方法中,基于TableA
的主键,你可以这样做;
Obj GetObj(string primaryKey)
{
dataBase context = new dataBase();
var obj = (from a
in context.TableA
where a.Id == primaryKey
select a);
var otherObj = (from b
in context.TableB
where b.Id == a.ForeignKey
select b);
Obj foo = new Obj();
foo.Id = a.Id;
foo.SomethingElse = a.Somthing;
foo.FromB = b.Id;
foo.AnInt = (int)b.count;
return foo;
}
要保存一些更改;
void AddNewObject()
{
dataBase context = new Context;
TableA a = new TableA();
TableB b = new TableB();
a.Id = this.Id;
a.OtherField = this.OtherField;
b.Key = this.BKey;
b.SomeInt = this.SomeInt;
context.AddObject("TableA", a);
context.AddObject("TableB", b);
context.SaveChanges();
}
当然这些都需要我没有包括一些错误处理,但概念不是从当你有标准EntityObject <不同 - >表的映射。我认为保留一些正常的映射然后在上面建立你的类是更简单的。
它是否反向工作?我可以给我的复杂对象,告诉它承诺,它会做拆除? I.E.沿着双向数据地图的线条。 – GPGVM
@ user1278561您必须编写另一种方法将对象属性映射到表及其属性。我会添加一个小例子。 – evanmcdonnal