我自己寫了一個庫來幫助進行通用數據庫查找。當我使用它時,我得到一個空白所有屬性的類。但是,基類正確填充。 product
變量已正確填充。如何讓代碼填充派生類(TModel entity
)?當我在DataService的Create
方法設置斷點(內部代碼註釋),這些都是在當地人結果/汽車窗口:基類填充而不是子
public abstract class GenericLookupViewModel
{
[Key]
public int ID { get; set; }
[Required]
[StringLength(300)]
public string Name { get; set; }
[Required]
public bool Active { get; set; }
[StringLength(50)]
[DisplayName("Record last modified by")]
public string ModifiedBy { get; set; }
[DisplayName("Record last modified Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime ModifiedOn { get; set; }
[StringLength(50)]
[DisplayName("Record created by")]
public string CreatedBy { get; set; }
[DisplayName("Record creation Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime CreatedOn { get; set; }
}
的TModel:基於GenericLookupViewModel類
public class GenericLookupModelDataService<TModel, TViewModel> : object, IDisposable
where TModel : GenericLookupModel, new()
where TViewModel : GenericLookupViewModel, new()
public virtual void Create(TViewModel product, string username = "SYSTEM")
{
TModel entity = new TModel
{
is_active = product.Active,
value = product.Name,
created_on = product.CreatedOn,
created_by = product.CreatedBy,
modified_on = product.ModifiedOn,
modified_by = product.ModifiedBy
};
if (string.IsNullOrEmpty(entity.created_by)) //breakpoint here
entity.SetCreated(username);
if (string.IsNullOrEmpty(entity.modified_by))
entity.SetModified(username);
_db.Set<TModel>().Add(entity);
_db.SaveChanges();
}
TViewModel基於GenericLookupModel類:
public abstract class GenericLookupModel : IActive, ICreated, IModified, IIdentity, IStringValue
{
public bool is_active { get; set; }
public string value { get; set; }
public DateTime created_on { get; set; }
public string created_by { get; set; }
public DateTime modified_on { get; set; }
public string modified_by { get; set; }
public int id {get;set;}
public void SetCreated(string creator = "SYSTEM")
{
created_by = creator;
created_on = DateTime.Now;
}
public void SetModified(string modifier = "SYSTEM")
{
modified_by = modifier;
modified_on = DateTime.Now;
}
public void ToggleActive()
{
is_active = !is_active;
}
}
控制器和行動:
public class PrimaryFocusController : GenericLookupViewModelController<PrimaryFocusViewModel,tblkp_PrimaryFocus>
{
public override ActionResult Create(PrimaryFocusViewModel lookup)
{
SetBrowsingUser(AppUser.Login);
return base.Create(lookup);
}
}
我編譯時得到警告可能必須做這樣的東西的信息庫:
warning CS0108: 'DataLayer.tblkp_PrimaryFocus.id' hides inherited member 'MyLib.Model.GenericLookupModel.id'. Use the new keyword if hiding was intended. warning CS0108: 'DataLayer.tblkp_PrimaryFocus.value' hides inherited member 'MyLib.Model.GenericLookupModel.value'. Use the new keyword if hiding was intended. warning CS0108: 'DataLayer.tblkp_PrimaryFocus.is_active' hides inherited member 'MyLib.Model.GenericLookupModel.is_active'. Use the new keyword if hiding was intended. warning CS0108: 'DataLayer.tblkp_PrimaryFocus.created_on' hides inherited member 'MyLib.Model.GenericLookupModel.created_on'. Use the new keyword if hiding was intended. warning CS0108: 'DataLayer.tblkp_PrimaryFocus.created_by' hides inherited member 'MyLib.Model.GenericLookupModel.created_by'. Use the new keyword if hiding was intended. warning CS0108: 'DataLayer.tblkp_PrimaryFocus.modified_on' hides inherited member 'MyLib.Model.GenericLookupModel.modified_on'. Use the new keyword if hiding was intended. warning CS0108: 'DataLayer.tblkp_PrimaryFocus.modified_by' hides inherited member 'MyLib.Model.GenericLookupModel.modified_by'. Use the new keyword if hiding was intended.
的DataLayer.tblkp_PrimaryFocus是EF生成的類使用DB First方法。
UPDATE:用戶@ code4life帶來了一個好點的 - 有標記爲virtual
(tblkp_PrimaryFocus)子類的所有屬性,但是這將意味着,我需要以紀念所有的人每次重新生成模型從EF圖 - 這就是我想要避免的 - 修改EF生成的類。
您需要將您想要在子類中重寫的屬性標記爲「虛擬」... – code4life
問題是子類是由Entity Framework動態生成的。如果我開始修改這些模型,那麼每次從EF圖生成模型時都需要修改它們,我想避免這種情況。 –