How to set ForeignKey column name in fluent API on EF Core

c# entity-framework entity-framework-core

Question

I'm trying to give my Foreign Key column the name I want.

If I set my foreignKey with the builder.Property, aka

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

, when I create relationship, I have an error on migration : "The navigation property 'LettreClePrincipale' cannot be added to the entity type 'LettreCleAssociation' because a property with the same name already exists on entity type 'LettreCleAssociation'."

If I don't set my foreignKey with the builder.Property, this error is gone, the key is correctly created, but I cannot set name during the process :

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

I've defined my model like this (whit many more properties)

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; }
        }

I don't have ForSqlServerHasName or ForSqliteHasName extensions method here. How can I proceed to achieve this ?

Running on EFCore 1.1 preview 1.

1
0
10/27/2016 2:34:58 PM

Popular Answer

You have two relations between LTC and LTCLinks and I guess EF doen't know which one to use in LTC.Links collection... Check InverseProperty attribute, you should have something like:

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

About your original question, here is code that works:

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");
            });
        }
    }
}

With this in 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": {}
  }

After generating and running migration I have in SQL:

enter image description here

1
10/27/2016 4:04:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow