c# - How do I map to and from a complex type in EF4.3 code-first? -
i've got entity type this:
public class invoice{ public int id { get; set; } public invoicenumbersequence sequence { get; set; } public decimal amount { get; set; } } the invoicenumbersequence looks this:
public class invoicenumbersequence { public string prefix { get; set; } public int number { get; set; } public string getsequence() { return prefix + number; } } my problem have existing database cannot change , i'm trying map tables/columns domain model. here's how table looks:
[systeminvoices] invoice_id int inv_total decimal invoice_seq varchar(255) i have dbcontext this:
public mydatabasecontext : dbcontext { public dbset<invoice> invoices { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<predictedheat>().totable("systeminvoices"); modelbuilder.entity<invoice>().haskey(p => p.id); modelbuilder.entity<invoice>().property(p => p.id).hascolumnname("invoice_id"); modelbuilder.entity<invoice>().property(p => p.amount).hascolumnname("inv_total"); //need here map invoice sequence database table } } i need map invoicenumbersequence both directions... 1) database field invoicenumbersequence class, , 2) invoicenumbersequence.getsequence() method database field.
how can this?
so instead of getsequence use property:
public class invoicenumbersequence { public string prefix { get; set; } public int number { get; set; } public string sequence { { retrun prefix + number; } set { // add parsing logic } } } and in mapping add:
modelbuilder.complextype<invoicenumbersequence>() .property(p => p.sequence) .hascolumnname("invoice_seq"); modelbuilder.complextype<invoicenumbersequence>() .ignore(p => p.prefix); modelbuilder.complextype<invoicenumbersequence>() .ignore(p => p.number);
Comments
Post a Comment