Entity Framework 6 ReferentialConstraint 中的依赖项 属性 映射到存储生成的列。错误
Entity Framework 6 A dependent property in a ReferentialConstraint is mapped to a store-generated column. Error
我使用 Entity Framework 6 配置了 2 个实体。
两个实体都具有用于在插入时生成主键的身份。
当我尝试添加新客户时出现以下错误。
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'.
我的假设是我没有为一对一关系正确配置其中一个实体。
public class Customer : IdEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Guid? BalanceId { get; set; }
public DateTime? Dob { get; set; }
public DateTime CreateDate { get; set; }
public CustomerBalance Balance { get; set; }
public Address Address { get; set; }
public virtual ICollection<Email> Emails { get; set; } = new List<Email>();
public virtual ICollection<Phone> Phones { get; set; } = new List<Phone>();
public virtual ICollection<Reward> Rewards { get; set; }
public ICollection<Call> Calls { get; set; }
}
这是客户
的映射
public class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
public CustomerConfiguration()
{
ToTable(nameof(Customer));
HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x => x.Phones);
HasMany(x => x.Emails);
HasOptional(x => x.Balance);
HasRequired(x => x.Address).WithRequiredDependent(x=>x.Customer);
}
}
这是地址实体
public class Address : IdEntity
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
和映射
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
public AddressConfiguration()
{
ToTable(nameof(Address));
HasKey(x => x.Id, e => e.IsClustered())
.Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
这是我如何插入新客户的代码
var customer = new Customer
{
FirstName = request.FirstName,
LastName = request.LastName,
CreateDate = DateTime.Now,
Address = new Address()
};
if (!string.IsNullOrEmpty(request.Email))
customer.Emails.Add(new Email { EmailName = request.Email, IsPrimary = true });
if (!string.IsNullOrEmpty(request.Phone))
customer.Phones.Add(new Phone { Number = request.Phone, IsPrimary = true });
_repository.AddOneAsync(customer);
await _repository.Context.SaveChangesAsync();
保存更改时出错。
Address 和 Customer 是一对一的关系。
这是我的表在 SQL
中的配置方式
EF6 中的一对一关系必须使用相同的键。 EG Address.CustomerId 必须是它的关键。
要么允许客户在模型中有多个地址,要么将 Address 的键更改为 CustomerID。
我使用 Entity Framework 6 配置了 2 个实体。 两个实体都具有用于在插入时生成主键的身份。
当我尝试添加新客户时出现以下错误。
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'Id'.
我的假设是我没有为一对一关系正确配置其中一个实体。
public class Customer : IdEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Guid? BalanceId { get; set; }
public DateTime? Dob { get; set; }
public DateTime CreateDate { get; set; }
public CustomerBalance Balance { get; set; }
public Address Address { get; set; }
public virtual ICollection<Email> Emails { get; set; } = new List<Email>();
public virtual ICollection<Phone> Phones { get; set; } = new List<Phone>();
public virtual ICollection<Reward> Rewards { get; set; }
public ICollection<Call> Calls { get; set; }
}
这是客户
的映射public class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
public CustomerConfiguration()
{
ToTable(nameof(Customer));
HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x => x.Phones);
HasMany(x => x.Emails);
HasOptional(x => x.Balance);
HasRequired(x => x.Address).WithRequiredDependent(x=>x.Customer);
}
}
这是地址实体
public class Address : IdEntity
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
和映射
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
public AddressConfiguration()
{
ToTable(nameof(Address));
HasKey(x => x.Id, e => e.IsClustered())
.Property(x => x.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
这是我如何插入新客户的代码
var customer = new Customer
{
FirstName = request.FirstName,
LastName = request.LastName,
CreateDate = DateTime.Now,
Address = new Address()
};
if (!string.IsNullOrEmpty(request.Email))
customer.Emails.Add(new Email { EmailName = request.Email, IsPrimary = true });
if (!string.IsNullOrEmpty(request.Phone))
customer.Phones.Add(new Phone { Number = request.Phone, IsPrimary = true });
_repository.AddOneAsync(customer);
await _repository.Context.SaveChangesAsync();
保存更改时出错。 Address 和 Customer 是一对一的关系。
这是我的表在 SQL
中的配置方式EF6 中的一对一关系必须使用相同的键。 EG Address.CustomerId 必须是它的关键。
要么允许客户在模型中有多个地址,要么将 Address 的键更改为 CustomerID。