没有导航性能外键值键值、性能

2023-09-07 10:34:00 作者:小白兔不乖

是否有可能在插入记录时手动分配外键值?

我不希望使用一个TransactionScope或类似的结构。但我想调用的SaveChanges之前设置的外键的值()

例如:

  EntityX X =新EntityX();
x.Name =测试;
ctx.AddToEntityX(X);

EntityY Y =新EntityY();
y.Name =测试;
y.EntityXID = x.ID; //< ---我想这一点。不使用导航属性,但它的0。
ctx.AddToEntityY(Y);

ctx.SaveChanges();
 

解决方案 三星G810有无GPS导航功能

如果您创建一个新的实体,它不会有一个ID,直到它得到的坚持。然后,你将不得不从数据库中检索,并得到了主意。使用导航属性肯定是在这个例子中你最好的选择。因此,而不是:

  y.EntityXID = x.ID;
 

你可以使用

  y.EntityX = X;
 

Is it possible to assign foreign key values manually when inserting records?

I do not want to use a TransactionScope or similar construct. But i do want to set the foreignkey value before calling SaveChanges()

For example:

EntityX x = new EntityX();
x.Name = "test";
ctx.AddToEntityX(x);

EntityY y = new EntityY();
y.Name = "Test";
y.EntityXID = x.ID; // <--- I want this. Not using a navigation property, but its 0.
ctx.AddToEntityY(y);

ctx.SaveChanges();

解决方案

If you create a new entity it won't have an ID until it get's persisted. Then you would have to retrieve it from the DB and get the idea. Using navigation properties is definitely your best choice in this example. So instead of:

y.EntityXID = x.ID;

you would use

y.EntityX = x;