【发布时间】:2013-11-20 15:30:08
【问题描述】:
我的数据库中有一个名为 CarDesign 的现有表,它与 Project 具有一对一的关系。我想添加一个名为 Design 的基表以在 TPH 表中使用。所以我可以为像 BoatDesign 这样的东西添加鉴别器。问题是实体框架只想删除旧表并创建一个新表。如何保留我的数据并使用鉴别器来表示当前记录?
我正在使用带有 SQL 的实体框架 5。如果这有助于解决问题,我还将更新到实体框架 6。项目是带有c#的asp.net MVC4
Mapping
public class CarDesignMap : EntityTypeConfiguration<CarDesign>
{
public CarDesignMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
//this.Property(t => t.Name)
//.IsRequired();
this.Property(t => t.RowVersion)
.IsRequired()
.IsFixedLength()
.HasMaxLength(8)
.IsRowVersion();
// Relationships
this.HasMany(t => t.InputVoltages)
.WithMany(t => t.CarDesign)
.Map(m =>
{
m.ToTable("InputVoltageCarDesign");
m.MapLeftKey("CarDesign_Id");
m.MapRightKey("InputVoltage_Id");
});
this.HasMany(t => t.LensColors)
.WithMany(t => t.CarDesign)
.Map(m =>
{
m.ToTable("LensCarDesign");
m.MapLeftKey("CarDesign_Id");
m.MapRightKey("LensColor_Id");
});
this.HasMany(t => t.LightSourceColors)
.WithMany(t => t.CarDesign)
.Map(m =>
{
m.ToTable("LightSourceColorCarDesign");
m.MapLeftKey("CarDesign_Id");
m.MapRightKey("LightSourceColor_Id");
});
this.HasMany(t => t.Standards)
.WithMany(t => t.CarDesign)
.Map(m =>
{
m.ToTable("StandardCarDesign");
m.MapLeftKey("CarDesign_Id");
m.MapRightKey("Standard_Id");
});
this.HasOptional(t => t.LightSource)
.WithMany(t => t.CarDesign)
.HasForeignKey(d => d.LightSourceId);
}
}
型号
public class CarDesign
{
public CarDesign()
{
Approvals = new List<Approval>();
Standards = new List<Standard>();
Connectors = new List<Connector>();
InputVoltages = new List<InputVoltage>();
LensColors = new List<LensColor>();
LightSourceColors = new List<LightSourceColor>();
}
[Required]
[Display(Name = "Product Name")]
public string Name { get; set; }
public PdsStatus Status { get; set; }
public Guid SubmittedById { get; set; }
[Display(Name = "Submitted By")]
public virtual User SubmittedBy { get; set; }
public Guid? ApprovedById { get; set; }
[Display(Name = "Approved By")]
public virtual User ApprovedBy { get; set; }
[Display(Name = "Approval Date")]
[DataType(DataType.Date)]
public DateTime? ApprovalDate { get; set; }
[Display(Name = "Submit Date")]
[DataType(DataType.Date)]
public DateTime SubmittalDate { get; set; }
public string SubmittalDateDisplay
{
get
{
return (SubmittalDate == null) ? "Not Set" : ((DateTime)SubmittalDate).ToString("MM/dd/yy");
}
}
public string Description { get; set; }
[Display(Name = "Market and Uses")]
public string MarketAndUses{get;set;}
[Display(Name = "Target M & L")]
[DataType(DataType.Currency)]
public double? TargetPrice { get; set; }
[Display(Name = "Annual Qty")]
public int? AnnualQuantities { get; set; }
[Display(Name = "Current Draw")]
public double? CurrentDraw { get; set; }
[Display(Name = "Light Source")]
public int? LightSourceId { get; set; }
public virtual LightSource LightSource { get; set; }
public LengthUnitOfMeasure LengthUnitOfMeasure { get; set; }
public WeightUnitOfMeasure WeightUnitOfMeasure { get; set; }
public PhotometricIntensityUnitOfMeasure LightIntensityUnitOfMeasure{ get; set; }
public virtual ICollection<Approval> Approvals { get; set; }
public virtual ICollection<Standard> Standards { get; set; }
public virtual ICollection<Connector> Connectors { get; set; }
public virtual ICollection<InputVoltage> InputVoltages { get; set; }
public virtual ICollection<LensColor> LensColors { get; set; }
public virtual ICollection<LightSourceColor> LightSourceColors { get; set; }
}
}
所以我很想拥有
公共课 CarDesign:设计
{
}
【问题讨论】:
-
你能给我们看一些代码吗?
标签: c# sql entity-framework entity-framework-6