根据上图,我有一个要求,表C需要同时引用表A和表B。
注意::Table_C中的RefId
是表A和表B的参考键。
请参考代码段
Table_A类
public partial class Table_A
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Table_C> Table_C { get; set; }
}
Table_B类
public partial class Table_B
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Table_C> Table_C { get; set; }
}
Table_C类
public partial class Table_C
{
public int Id { get; set; }
public int RefId { get; set; }
public Type Type {get; set; }
public virtual Table_A Table_A { get; set; }
public virtual Table_B Table_B { get; set; }
}
Fluent API
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Table_1>()
.Property(e => e.Name)
.IsFixedLength();
modelBuilder.Entity<Table_1>()
.HasMany(e => e.Table_3)
.WithRequired(e => e.Table_1)
.HasForeignKey(e => e.RefId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Table_2>()
.Property(e => e.Name)
.IsFixedLength();
modelBuilder.Entity<Table_2>()
.HasMany(e => e.Table_3)
.WithRequired(e => e.Table_2)
.HasForeignKey(e => e.RefId)
.WillCascadeOnDelete(false);
}
但是,使用上面显示的代码尝试达到此要求时,会发生以下错误。
INSERT语句与FOREIGN KEY约束冲突 “ FK_dbo.TableC.TableA_Id”。数据库“ TestDB”中发生了冲突, 表“ dbo.TableA”的“ Id”列。该声明已终止
如何使用Entity Framework 6(SQL Server 2014,.NET Framework 4.6.1)实现此功能?
答案 0 :(得分:1)
似乎对我来说很好:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ef6Test
{
public partial class Table_A
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Table_C> Table_C { get; } = new HashSet<Table_C>();
}
//Table_B Class
public partial class Table_B
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Table_C> Table_C { get; } = new HashSet<Table_C>();
}
//Table_C Class
public partial class Table_C
{
public int Id { get; set; }
public int RefId { get; set; }
public string Type { get; set; }
public virtual Table_A Table_A { get; set; }
public virtual Table_B Table_B { get; set; }
}
class Db: DbContext
{
public DbSet<Table_A> Table_A { get; set; }
public DbSet<Table_B> Table_B { get; set; }
public DbSet<Table_C> Table_C { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Table_A>()
.HasMany(e => e.Table_C)
.WithRequired(e => e.Table_A)
.HasForeignKey(e => e.RefId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Table_B>()
.HasMany(e => e.Table_C)
.WithRequired(e => e.Table_B)
.HasForeignKey(e => e.RefId)
.WillCascadeOnDelete(false);
}
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<Db>());
using (var db = new Db())
{
db.Database.Log = m => Console.WriteLine(m);
db.Database.Initialize(true);
var a = new Table_A();
var b = new Table_B();
var c = new Table_C();
a.Table_C.Add(c);
b.Table_C.Add(c);
db.Table_A.Add(a);
db.Table_B.Add(b);
db.Table_C.Add(c);
db.SaveChanges();
}
Console.ReadKey();
}
}
}
答案 1 :(得分:0)
我使用@David Browne - Microsoft的评论设法满足了这一要求。因此,使用多列来引用多个相关表是这种情况的方法。希望这会对寻求此类问题答案的任何人有所帮助。感谢@David Browne - Microsoft的宝贵意见。