没有导航性能外键值 [英] Foreign key values without navigation properties

查看:125
本文介绍了没有导航性能外键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我不希望使用一个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();
 

解决方案

如果您创建一个新的实体,它不会有一个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;

这篇关于没有导航性能外键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆