1

我有两个类 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() 方法本身,并且一切正常。谁能解释发生了什么?

4

0 回答 0