如何在EF Core上的Fluent API中設置ForeignKey列名

c# entity-framework entity-framework-core

我試圖給我的外鍵列我想要的名字。

如果我用builder.Property設置我的foreignKey,又名

var b = builder.Property(column.PropertyType, column.PropertyName);
b.ForSqlServerHasName("MyFKColumnName");

當我創建關係時,我在遷移時出錯:“導航屬性'LettreClePrincipale'無法添加到實體類型'LettreCleAssociation',因為實體類型'LettreCleAssociation'上已經存在具有相同名稱的屬性。”

如果我沒有使用builder.Property設置我的foreignKey,則此錯誤消失,密鑰已正確創建,但我無法在此過程中設置名稱:

ltcLinkBuilder.HasOne(typeof(LTC).FullName, "Link1")
.WithMany().IsRequired()
.HasForeignKey("Link1ID").OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Cascade)
.???

我已經定義了這樣的模型(更多屬性)

class LTC
        {
            public int TestA { get; set; }
            public string TestB { get; set; }

            public Guid Id { get; set; }

            protected ICollection<LTCLinks> _links { get; set; }

            public IReadOnlyList<LTCLinks> Links
            {
                get
                {
                    return _links.ToList().AsReadOnly();
                }
            }
        }

        class LTCLinks
        {
            public Guid Link1ID { get; set; }
            public LTC Link1 { get; set; }
            public Guid? Link2ID { get; set; }
            public LTC Link2 { get; set; }

            public string Data { get; set; }
        }

我這裡沒有ForSqlServerHasName或ForSqliteHasName擴展方法。我該如何著手實現這一目標?

在EFCore 1.1預覽版上運行1。

熱門答案

你有LTCLTCLinks之間的兩個關係,我想EF不知道在LTC.Links集合中使用哪一個......檢查InverseProperty屬性,你應該有類似的東西:

[InverseProperty("Link2")]
public IReadOnlyList<LTCLinks> Links {...}

關於您的原始問題,以下是有效的代碼:

namespace ConsoleApp1
{
    using Microsoft.EntityFrameworkCore;

    public class Foo
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }

    public class Bar
    {
        public int Id { get; set; }

        public int FooId { get; set; }

        public virtual Foo Foo { get; set; }
    }

    public class MyDb : DbContext
    {
        public DbSet<Foo> Foos { get; }

        public DbSet<Bar> Bars { get; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=test; Trusted_Connection=True");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Foo>(t =>
            {
                t.HasKey(x => x.Id);
            });

            modelBuilder.Entity<Bar>(t =>
            {
                t.HasKey(x => x.Id);
                t.Property(x => x.FooId).HasColumnName("FooIdColumn");
                t.HasOne(x => x.Foo).WithMany().HasForeignKey(x => x.FooId).HasConstraintName("MyFK");
            });
        }
    }
}

project.json有這個:

  "dependencies": {
    "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" },
    "Microsoft.EntityFrameworkCore.Tools": { "type": "build", "version": "1.0.0-preview3-final" },
    "Microsoft.EntityFrameworkCore.Design": { "type": "build", "version": "1.1.0-preview1-final" },
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0-preview1-final"
  },

  "tools": { "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final" },

  "frameworks": {
    "netcoreapp1.0": {}
  }

生成並運行遷移後,我在SQL中:

在此處輸入圖像描述



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow