我正在使用實體框架代碼優先方法。我有以下代碼將數據插入到PaymentComponent和Payment表中。插入到PaymentComponent表中的數據不正確。即使域對象中的相應屬性不爲空,它也會在兩列(對於一條記錄)中具有NULL值。需要改變什麼才能使其工作?實體框架代碼優先:插入數據庫的NULL值
編輯
當我添加了以下的NerdDinners類,我得到下面的結果 - 它有新的不需要的列
public DbSet<ClubCardPayment> ClubCardPayments { get; set; }
原碼
static void Main(string[] args)
{
string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
using (var db = new NerdDinners(connectionstring))
{
GiftCouponPayment giftCouponPayment = new GiftCouponPayment();
giftCouponPayment.MyValue=250;
giftCouponPayment.MyType = "GiftCouponPayment";
ClubCardPayment clubCardPayment = new ClubCardPayment();
clubCardPayment.MyValue = 5000;
clubCardPayment.MyType = "ClubCardPayment";
List<PaymentComponent> comps = new List<PaymentComponent>();
comps.Add(giftCouponPayment);
comps.Add(clubCardPayment);
var payment = new Payment { PaymentComponents = comps, PayedTime=DateTime.Now };
db.Payments.Add(payment);
int recordsAffected = db.SaveChanges();
}
}
域碼
public abstract class PaymentComponent
{
public int PaymentComponentID { get; set; }
public abstract int MyValue { get; set; }
public abstract string MyType { get; set; }
public abstract int GetEffectiveValue();
}
public partial class GiftCouponPayment : PaymentComponent
{
private int couponValue;
private string myType;
public override int MyValue
{
get
{
return this.couponValue;
}
set
{
this.couponValue = value;
}
}
public override string MyType
{
get
{
return this.myType;
}
set
{
this.myType = value;
}
}
public override int GetEffectiveValue()
{
if (this.PaymentComponentID < 2000)
{
return 0;
}
return this.couponValue;
}
}
public partial class ClubCardPayment : PaymentComponent
{
private int cardValue;
private string myType;
public override int MyValue
{
get
{
return this.cardValue;
}
set
{
this.cardValue = value;
}
}
public override string MyType
{
get
{
return this.myType;
}
set
{
this.myType = value;
}
}
public override int GetEffectiveValue()
{
return this.cardValue;
}
}
public partial class Payment
{
public int PaymentID { get; set; }
public List<PaymentComponent> PaymentComponents { get; set; }
public DateTime PayedTime { get; set; }
}
//System.Data.Entity.DbContext is from EntityFramework.dll
public class NerdDinners : System.Data.Entity.DbContext
{
public NerdDinners(string connString): base(connString)
{
}
protected override void OnModelCreating(DbModelBuilder modelbuilder)
{
modelbuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public DbSet<GiftCouponPayment> GiftCouponPayments { get; set; }
public DbSet<Payment> Payments { get; set; }
}
REFERENCE:
- When using entity framework code-first mapping property to separate table, moves foreign key field
- Override Entity Framework Entity Property
- EntityFramework how to Override properties
- http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx
- http://www.robbagby.com/entity-framework/entity-framework-modeling-entity-splitting/
- 實體框架映射方案 - http://msdn.microsoft.com/en-us/library/cc716779.aspx
- http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/06/entity-splitting-in-entity-framework.aspx
謝謝。這是否意味着在EF中不支持重寫屬性? – Lijo 2012-07-24 08:44:09