EFCore 拥有的模型具有映射到 foreignKey 的对象属性

问题描述 投票:0回答:1

我有一个模型,其中有 3 个类(完整模型的一部分)

owned
班级有
ForeignKey
到第三班级时,我想在同一张桌子上有2个模型班级。
但是当我尝试配置
ForeignKey
时,我得到了错误:
(0x80131904): Invalid column name 'Customer_city_id'.

我无法更改模型或表格,只能更改
context
.
型号:

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string District { get; set; }
}

public class Customer
{
    public string? Id { get; set; }
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public City? City { get; set; }
}

public class Order
{
    public int Id { get; set; }//OrderId
    public Customer Customer { get; set; }       
    //more owntype properties
}

表:

tbl_customers
-----------------------------------------------------------
id | customer_id | customer_first_name | customer_last_name
tbl_city
-----------------------------------------------------------
city| city_desc| dist

上下文:

public class EntityContext: DbContext
{
    public DbSet<Order> Orders { get; set; }
    //public DbSet<Customer> Customers { get; set; }
    

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer("myConnectionString");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        
        modelBuilder.Entity<City>(c =>
        {
            c.ToTable("tbl_city")
            .HasKey(c=>c.Id);

            c.Property(c => c.Id)
            .HasColumnName("city");

            c.Property(c => c.Name)
            .HasColumnName("city_desc");

            c.Property(c => c.District)
            .HasColumnName("dist");
        });
        modelBuilder.Entity<Order>(c =>
        {
            c.ToTable("tbl_customers");
            c.Property(c=>c.Id).HasColumnName("id");
            c.OwnsOne(p => p.Customer, s =>
                {
                    s.ToTable("tbl_customers");
                    s.Property(c => c.Id)
                    .HasColumnName("customer_id");
                    s.Property(c => c.FirstName)
                        .HasColumnName("customer_first_name");
                    s.Property(c => c.LastName)
                        .HasColumnName("customer_last_name");

                //---------get error->

                // s.HasOne<City>()
                //  .WithMany("city")
                //  .HasForeignKey("city_id");

                
                });
        }); 

        
    }
}

但我得到错误:

(0x80131904): Invalid column name 'Customer_city_id'.
   at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__188_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)                    
entity-framework-core relational-database c#-7.0 object-property ef-model-builder
1个回答
0
投票

自有类型的 Microsoft 文档:

EF Core 允许您对只能出现在其他实体类型的导航属性上的实体类型进行建模。这些称为拥有的实体类型。包含拥有的实体类型的实体是它的所有者。Source

因此,此类属性不允许使用 HasOne 配置,因为这意味着它们可能出现在不拥有它们的实体上,这将破坏目的。

相反,我建议使用

HasOne/WithMany
而不是
OwnsOne
,因为 OwnsOne 意味着客户是订单的一部分,没有它就无法存在(这可能不是您想要的)。

© www.soinside.com 2019 - 2024. All rights reserved.