Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 定义实体之间的主键关系时,EF Core 3.1会创建名称为“1”的重复列_C#_Sql Server_Entity Framework - Fatal编程技术网

C# 定义实体之间的主键关系时,EF Core 3.1会创建名称为“1”的重复列

C# 定义实体之间的主键关系时,EF Core 3.1会创建名称为“1”的重复列,c#,sql-server,entity-framework,C#,Sql Server,Entity Framework,我在SQLServer中有一个非常简单的测试数据库,它有两个表:蛋糕和口味。一种味道可以对应许多蛋糕。每个蛋糕记录都有一个可空的FlavorType列,该列对应于Flavors表中的FlavorType列,该列不是Flavors表的主键 当我尝试在EF Core中对这种关系建模时,在构建数据上下文时,它会在我的Cake实体上创建一个额外的FlavorType1 shadow属性。然后,当我尝试使用相当于SELECT*FROM的方法来访问非常简单的OData服务时,查询失败,因为它在要选择的列列表

我在SQLServer中有一个非常简单的测试数据库,它有两个表:蛋糕和口味。一种味道可以对应许多蛋糕。每个蛋糕记录都有一个可空的FlavorType列,该列对应于Flavors表中的FlavorType列,该列不是Flavors表的主键

当我尝试在EF Core中对这种关系建模时,在构建数据上下文时,它会在我的Cake实体上创建一个额外的FlavorType1 shadow属性。然后,当我尝试使用相当于SELECT*FROM的方法来访问非常简单的OData服务时,查询失败,因为它在要选择的列列表中包含了这个不存在的FlavorType1列

如果我将Cakes上的FlavorType列重命名为其他名称,如FType,那么EF核心数据上下文将毫无问题地构建,并且我的查询将成功运行。只有当香料的主键列和蛋糕的相关列具有相同的名称时,才会出现此问题。有什么建议可以解决这个问题吗?这种情况在我正在查看的工作项目中经常发生,我无法确保principal key列和相关实体的列必须具有不同的名称

这是一个例子。 下面是Cake.cs模型:

[Table("Cakes")]
public class Cake
{
    [Key]
    public int CakeUniqueIdentifier { get; set; }

    public string Description { get; set; }

    public string FlavorType { get; set; }

    public Flavor Flavor { get; set; }
}
[Table("Flavors")]
public class Flavor
{
    public Flavor()
    {
        Cakes = new HashSet<Cake>();
    }

    [Key]
    public int Id { get; set; }

    public string FlavorType { get; set; }

    public string Description { get; set; }

    public IEnumerable<Cake> Cakes { get; set; }
}
Flavor.cs模型:

[Table("Cakes")]
public class Cake
{
    [Key]
    public int CakeUniqueIdentifier { get; set; }

    public string Description { get; set; }

    public string FlavorType { get; set; }

    public Flavor Flavor { get; set; }
}
[Table("Flavors")]
public class Flavor
{
    public Flavor()
    {
        Cakes = new HashSet<Cake>();
    }

    [Key]
    public int Id { get; set; }

    public string FlavorType { get; set; }

    public string Description { get; set; }

    public IEnumerable<Cake> Cakes { get; set; }
}
运行应用程序时生成的模型的调试视图:

  EntityType: Cake
    Properties: 
      CakeUniqueIdentifier (int) Required PK AfterSave:Throw ValueGenerated.OnAdd
        Annotations: 
          SqlServer:ValueGenerationStrategy: IdentityColumn
          TypeMapping: Microsoft.EntityFrameworkCore.Storage.IntTypeMapping
      Description (string)
        Annotations: 
          TypeMapping: Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerStringTypeMapping
      FlavorType (string)
        Annotations: 
          TypeMapping: Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerStringTypeMapping
      FlavorType1 (no field, string) Shadow FK Index
        Annotations: 
          TypeMapping: Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerStringTypeMapping
    Navigations: 
      Flavor (Flavor) ToPrincipal Flavor Inverse: Cakes
    Keys: 
      CakeUniqueIdentifier PK
    Foreign keys: 
      Cake {'FlavorType1'} -> Flavor {'FlavorType'} ToDependent: Cakes ToPrincipal: Flavor
    Annotations: 
      ConstructorBinding: Microsoft.EntityFrameworkCore.Metadata.ConstructorBinding
      Relational:TableName: Cakes
  EntityType: Flavor
    Properties: 
      Id (int) Required PK AfterSave:Throw ValueGenerated.OnAdd
        Annotations: 
          SqlServer:ValueGenerationStrategy: IdentityColumn
          TypeMapping: Microsoft.EntityFrameworkCore.Storage.IntTypeMapping
      Description (string)
        Annotations: 
          TypeMapping: Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerStringTypeMapping
      FlavorType (string) Required AlternateKey AfterSave:Throw
        Annotations: 
          TypeMapping: Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerStringTypeMapping
    Navigations: 
      Cakes (IEnumerable<Cake>) Collection ToDependent Cake Inverse: Flavor
    Keys: 
      FlavorType
      Id PK
    Annotations: 
      ConstructorBinding: Microsoft.EntityFrameworkCore.Metadata.ConstructorBinding
      Relational:TableName: Flavors
Annotations: 
  ProductVersion: 3.1.3
  Relational:MaxIdentifierLength: 128
  SqlServer:ValueGenerationStrategy: IdentityColumn```



哎呀。。。发现了发生的事情。我不得不将此添加到Fluent配置中:.HasForeignKeyFlavorType,现在模型按预期构建

modelBuilder.Entity<Cake>()
            .HasOne(t => t.Flavor)
            .WithMany(s => s.Cakes)
            .HasPrincipalKey("FlavorType")
            .HasForeignKey("FlavorType");

删除fluent配置。@Wouter,很遗憾,这不起作用。第一个指定Cake实体主键的fluent配置语句必须在那里,我认为这是因为Id列的有意非标准命名;删除它会导致没有任何实体的空模型。删除指定主键关系的第二个fluent配置会导致查询失败,因为按照约定将阴影“FlavorId”列添加到蛋糕实体中。EF不会基于Flavor实体中的非主键列自动检测关系。