如何将1个表中的多个列与另一个表中的同一列相关联? [英] How to relate multiple columns in 1 table to the same column in another table ?
问题描述
有两个表:tblA和tblB。
我在tblA中添加了新列(intNewCol1和intNewCol2),这两个列在tblB中是intID
这是tblB表
[TableName(" tblBs")]
public class tblB
{
[StringLength(100)]
public string说明{get;组; }
[Key]
public int intID {get;组; }
}
我如何定义tblA,以便将
intNewCol1与tblB中的intID绑定,
和
$
intNewCol2与tblB中的intID绑定为
?
此外,intNewCol1 和/或intNewCol2在tblA中可以为NULL
tblA
public int? intNewCol1 {get;组;公共int? intNewCol2 {get;组; }
谢谢
嗨aujong,
由于Entity Framework依赖于每个拥有密钥的实体它用于跟踪实体的值。所以tblA会这样:
public partial class tblA
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id {get;组; }
public int? intNewCol1 {get;组; }
public int? intNewCol2 {get;组; }
public virtual tblB tblB {get;组; }
public virtual tblB tblB1 {get;组; }
}
#Table相关
CREATE TABLE [dbo]。[tblAs](
[Id] INT NOT NULL,
[intNewCol1] INT NULL,
[intNewCol2] INT NULL
);#Fluent API
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< tblB>()
.Property(e => e.Description)
.IsUnicode(false);
modelBuilder.Entity< tblB>()
.HasMany(e => e.tblAs)
.WithOptional(e => e.tblB)
.HasForeignKey(e => e.intNewCol1);
modelBuilder.Entity< tblB>()
.HasMany(e => e.tblAs1)
.WithOptional(e => e.tblB1)
.HasForeignKey(e => e.intNewCol2);
}#tblB
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
公共部分类tblB
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(" Microsoft.Usage"," CA2214:DoNotCallOverridableMethodsInConstructors")]
public tblB()
{
tblAs = new HashSet< tblA>();
tblAs1 = new HashSet< tblA>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int intID {get;组; }
[StringLength(100)]
public string说明{get;组; }
[System.Diagnostics.CodeAnalysis.SuppressMessage(" Microsoft.Usage"," CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection< tblA> tblAs {get;组; }
[System.Diagnostics.CodeAnalysis.SuppressMessage(" Microsoft.Usage"," CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection< tblA> tblAs1 {get;组; }
}祝你好运,
Cole Wu
There are 2 tables: tblA and tblB.
I added new columns (intNewCol1 and intNewCol2) in tblA, and those 2 columns are intID in tblB
[TableName("tblBs")] public class tblB { [StringLength(100)] public string Description { get; set; } [Key] public int intID { get; set; } }
How can I define tblA so that
intNewCol1 is tied with intID in tblB,
and
intNewCol2 is tied with intID in tblB
?
Also, intNewCol1 and/or intNewCol2 can be NULL in tblA
tblA
public int? intNewCol1 { get; set; }
public int? intNewCol2 { get; set; }
Thank you
Hi aujong,
Since Entity Framework relies on every entity having a key value that it uses for tracking entities. So that tblA would like this:
public partial class tblA { [DatabaseGenerated(DatabaseGeneratedOption.None)] public int Id { get; set; } public int? intNewCol1 { get; set; } public int? intNewCol2 { get; set; } public virtual tblB tblB { get; set; } public virtual tblB tblB1 { get; set; } }
#Table Related
CREATE TABLE [dbo].[tblAs] ( [Id] INT NOT NULL, [intNewCol1] INT NULL, [intNewCol2] INT NULL );#Fluent API
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<tblB>() .Property(e => e.Description) .IsUnicode(false); modelBuilder.Entity<tblB>() .HasMany(e => e.tblAs) .WithOptional(e => e.tblB) .HasForeignKey(e => e.intNewCol1); modelBuilder.Entity<tblB>() .HasMany(e => e.tblAs1) .WithOptional(e => e.tblB1) .HasForeignKey(e => e.intNewCol2); }#tblB
using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; public partial class tblB { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public tblB() { tblAs = new HashSet<tblA>(); tblAs1 = new HashSet<tblA>(); } [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int intID { get; set; } [StringLength(100)] public string Description { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<tblA> tblAs { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<tblA> tblAs1 { get; set; } }Best regards,
Cole Wu
这篇关于如何将1个表中的多个列与另一个表中的同一列相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!