Sto provando a dare alla mia colonna Chiave esterna il nome che voglio.
Se imposto la mia foreignKey con il builder.Property, aka
var b = builder.Property(column.PropertyType, column.PropertyName);
b.ForSqlServerHasName("MyFKColumnName");
, quando creo una relazione, ho un errore di migrazione: "La proprietà di navigazione" LettreClePrincipale "non può essere aggiunta al tipo di entità" LettreCleAssociation "perché esiste già una proprietà con lo stesso nome sul tipo di entità" LettreCleAssociation "."
Se non ho impostato la mia foreignKey con builder.Property, questo errore è scomparso, la chiave è stata creata correttamente, ma non posso impostare il nome durante il processo:
ltcLinkBuilder.HasOne(typeof(LTC).FullName, "Link1")
.WithMany().IsRequired()
.HasForeignKey("Link1ID").OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Cascade)
.???
Ho definito il mio modello in questo modo (con molte altre proprietà)
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; }
}
Non ho qui il metodo di estensione ForSqlServerHasName o ForSqliteHasName. Come posso procedere per raggiungere questo obiettivo?
In esecuzione su EFCore 1.1 preview 1.
Hai due relazioni tra LTC
e LTCLinks
e suppongo che EF non sappia quale utilizzare nella raccolta LTC.Links
... Controlla l' attributo InverseProperty , dovresti avere qualcosa del tipo:
[InverseProperty("Link2")]
public IReadOnlyList<LTCLinks> Links {...}
Riguardo la tua domanda originale, ecco il codice che funziona:
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");
});
}
}
}
Con questo 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": {}
}
Dopo aver generato ed eseguito la migrazione ho in SQL: