传递 ObjectContext EF 4.0
我有两个类 Queries 和 ContactRepository。
public static class Queries
{
public static Contact GetContactByName(CEntities context, string name)
{
return (from Contact c in context.Contacts
where c.Name == name
select c).FirstOrDefault();
}
}
public class ContactRepository
{
private CEntities _dbContext;
public ContactRepository()
{
_dbContext = new CEntities();
}
public void CreateContactAddress(string contactName, string address, string city, string state, string zip)
{
int contactId;
ContactAddress ca = new ContactAddress();
contactId = Queries.GetContactByName(_dbContext, contactName).Id;
ca.ContactId = contactId;
ca.Address = address
ca.City = city;
ca.State = state;
ca.Zip = zip;
_dbContext.ContactAddresses.AddObject(ca);
_dbContext.SaveChanges();
}
}
当我调用 CreateContactAddress() 时,SaveChanges() 方法崩溃并出现以下错误:
无法确定有效的排序 对于相关操作。依赖关系 可能由于外键而存在 约束、模型要求或 存储生成的值。
我可以将 LINQ 查询移至 CreateContactAddress() 方法本身,一切都运行良好。谁能解释一下发生了什么事吗?
I have two classes Queries and ContactRepository.
public static class Queries
{
public static Contact GetContactByName(CEntities context, string name)
{
return (from Contact c in context.Contacts
where c.Name == name
select c).FirstOrDefault();
}
}
public class ContactRepository
{
private CEntities _dbContext;
public ContactRepository()
{
_dbContext = new CEntities();
}
public void CreateContactAddress(string contactName, string address, string city, string state, string zip)
{
int contactId;
ContactAddress ca = new ContactAddress();
contactId = Queries.GetContactByName(_dbContext, contactName).Id;
ca.ContactId = contactId;
ca.Address = address
ca.City = city;
ca.State = state;
ca.Zip = zip;
_dbContext.ContactAddresses.AddObject(ca);
_dbContext.SaveChanges();
}
}
When I call CreateContactAddress(), the SaveChanges() method crashes with the following error:
Unable to determine a valid ordering
for dependent operations. Dependencies
may exist due to foreign key
constraints, model requirements, or
store-generated values.
I can move the LINQ query into the CreateContactAddress() method itself, and everything works perfectly. Can anyone explain what is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论