ton*_*9uk 7 entity-framework ef-core-2.1
我真的是EF的新手(使用EF core 2.1),并且到目前为止已经关注了很多教程,但是现在我开始创建自己的数据库结构,并在尝试向数据库中添加值时偶然发现:
private async Task<int> Insert()
{
var address = new Address { AddressLine1 = "1 any street", AddressLine2 = "", AddressLine3 = "", City = "Any city" };
using (var context = new BranchContext())
{
context.Addresses.AddAsync(address);//ERROR HERE
....
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
InvalidOperationException:属性“ Branch.Address”的类型为“地址”,当前数据库提供程序不支持该类型。使用'[NotMapped]'属性或'OnModelCreating'中的'EntityTypeBuilder.Ignore'更改属性CLR类型或忽略该属性。
我创建了以下类:
public class Address
{
public int Id { get; set; }
public int Guid { get; set; }
public DateTime CreatedOn { get; set; }
public string Number { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string Town { get; set; }
public string City { get; set; }
public string Postcode1 { get; set; }
public string Postcode2 { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
}
public class Branch
{
public string Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
public IEnumerable<EmployeeBranch> Employee { get; set; }
public bool IsMain { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Guid { get; set; }
public string EmailAddress { get; set; }
public JobTitle JobTitle { get; set; }
public DateTime DateOfBirth { get; set; }
public IEnumerable<EmployeeBranch> Branches { get; set; }
public Branch PrimaryBranch { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PreferredName { get; set; }
public Salutations Salutation { get; set; }
}
public enum Salutations
{
Mr = 1,
Mrs = 2,
Miss = 3,
Ms = 4
}
public class EmployeeBranch
{
public int BranchId { get; set; }
public Branch Branch { get; set; }
public int EmployeeId { get; set; }
public Employee Employee { get; set; }
}
public class JobTitle
{
public int Id { get; set; }
public string Guid { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我跑了:
Add-Migration init
Update-Database
Run Code Online (Sandbox Code Playgroud)
创建了以下内容:
To be clear this is a runtime error, I have viewed couple of threads that get this error when they try to update their db here and here but their discussions have not pointed me in the right direction (maybe I'm missing the obvious).
How do I resolve this? Preferably without Channing the structure, although I'm happy to if there is a good reason to do so
Ale*_*xei 13
在引用导航属性而不是约束或类似内容中的映射属性的愚蠢情况下,也可能会弹出此错误:
modelBuilder.Entity<TheEntity>().HasKey(ma => new { ma.SomeKey, ma.NavPropInsteadOfProp });
Run Code Online (Sandbox Code Playgroud)
不幸的是,错误不够明确,但暂时从错误中注释掉罪魁祸首会导致源头。
最后看来这是一个相当简单的答案,这个错误让我查看了我的类的结构并试图找出出了什么问题。问题是,在我的BranchContext(我没有考虑在我的问题中分享),我OnModelCreating根据需要连接并设置了地址
错误的
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EmployeeBranch>()
.HasKey(s => new { s.BranchId, s.EmployeeId });
modelBuilder.Entity<Branch>()
.Property(s => s.Address).IsRequired();
modelBuilder.Entity<Branch>()
.Property(s => s.Name).IsRequired();
base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)
正确的
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EmployeeBranch>()
.HasKey(s => new { s.BranchId, s.EmployeeId });
modelBuilder.Entity<Address>()
.Property(s => s.AddressLine1).IsRequired();
modelBuilder.Entity<Address>()
.Property(s => s.Postcode1).IsRequired();
modelBuilder.Entity<Address>()
.Property(s => s.Postcode2).IsRequired();
...the other required elements...
modelBuilder.Entity<Branch>()
.Property(s => s.Name).IsRequired();
base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)
假设:目标是使Address所需的属性Branch
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Branch>()
.HasOne(s => s.Address)
.WithMany()
.IsRequired();
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 Fluent API 来配置关系是必需的还是可选的
来源:必需和可选的关系
| 归档时间: |
|
| 查看次数: |
1982 次 |
| 最近记录: |