代码之家  ›  专栏  ›  技术社区  ›  Marie

efcore2.1说已经向主键添加了多个列,但是它们没有

  •  1
  • Marie  · 技术社区  · 7 年前

    我试图使用efcore2.1创建一个关系模型,但是在添加迁移时,我收到一个错误消息:多个列被绑定到主键。看起来情况并非如此,我也不确定它为什么会这么认为。

    我可以用下面的一个非常简单的项目进行复制,这就是错误所在。

    “OwnedEntity”上的键{'ParentId'}和“Entity”上的{'Id'}都映射到'实体.PK_实体'但是有不同的列({'ParentId'}和{'Id'})。

    here

    删除ColumnAttribute允许添加迁移继续/成功,但我真的不希望 Owned

    using System.ComponentModel.DataAnnotations.Schema;
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace OwnedTypeTest
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                CreateWebHostBuilder(args).Build().Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
        }
        public class Startup
        {
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<TestDBContext>(o=>o.UseSqlServer("test"));
            }
    
            public void Configure(IApplicationBuilder app, IHostingEnvironment env) { }
        }
        public class TestDBContext : DbContext
        {
            public TestDBContext(DbContextOptions options) : base(options) { }
            public DbSet<Entity> Entities { get; set; }
        }
        public class Entity
        {
            public System.Guid Id { get; set; }
            public OwnedEntity Owned { get; set; }
        }
        [Owned]
        public class OwnedEntity
        {
            [Column(nameof(ParentId))]
            public System.Guid ParentId { get; set; }
            [Column(nameof(Parent))]
            public Entity Parent { get; set; }
        }
    }
    

    如果我像下面这样覆盖OnModelCreating,我会得到注释结果

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        Debugger.Launch();
        var et = modelBuilder.Model.GetEntityTypes(typeof(Entity)).First();
        var keys = et.GetKeys();
        var keyCount = keys.Count(); // 1
        var key = keys.First().ToString(); // Key: Entity.Id PK
        var properties = keys.First().Properties;
        var propertyCount = properties.Count(); // 1
        var property = properties.First().ToString(); // Property: Entity.Id (Guid) Required PK AfterSave:Throw ValueGenerated.OnAdd 0 0 0 -1 0
    }
    

    为了完整起见,这是我的csproj

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <Folder Include="wwwroot\" />
      </ItemGroup>
    
      <ItemGroup>
        <PackageReference Include="BuildBundlerMinifier" Version="2.6.375" />
        <PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.5" />
        <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.3" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.1.3" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.1.4" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.4" />
    
      </ItemGroup>
    
    </Project>
    

    这是一个bug还是我遗漏了EF的一些隐含行为?如果我是,发生了什么事?

    0 回复  |  直到 7 年前
    推荐文章