Sql server 如何将派生类添加到存储库
我需要一个建议。 我有两个类,其中一个是派生类。 我想在一个方法中插入我的基类和派生类。我怎样才能做到这一点Sql server 如何将派生类添加到存储库,sql-server,asp.net-mvc,entity-framework,design-patterns,repository,Sql Server,Asp.net Mvc,Entity Framework,Design Patterns,Repository,我需要一个建议。 我有两个类,其中一个是派生类。 我想在一个方法中插入我的基类和派生类。我怎样才能做到这一点 //my entities public class Form { public int FormId { get; set; } public string Name { get; set; } public string Surname { get; set; } public string Message { get; set; } } publi
//my entities
public class Form
{
public int FormId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Message { get; set; }
}
public class ReturnForm : Form
{
public int OrderId { get; set; }
}
//my repository
public class FormRepository : IFormRepository:IDisposable
{
private readonly MyContext _db;
public FormRepository(MyContext db)
{
_db = db;
}
//I want to insert here Form or ReturnForm objects.
//What can i do?
public void AddFormMessage(Form form)
{
_db.Forms.Add(form);
}
}
我已经在业务层解决了我的问题
public void AddFormMessage<T>(FormDto formDto)
{
Form form;
var targetClassType = typeof(T);
if (targetClassType == typeof(Form))
{
form = _mapper.Map<Form>(formDto);
}
else if (targetClassType == typeof(ReturnForm))
{
form = _mapper.Map<ReturnForm>(formDto);
}
else
{
throw new Exception("Unknown form type");
}
_unitOfWork.FormRepository.AddFormMessage(form);
}
public void AddFormMessage(FormDto-FormDto)
{
形式;
var targetClassType=类型(T);
if(targetClassType==typeof(Form))
{
form=\u mapper.Map(formDto);
}
else if(targetClassType==typeof(ReturnForm))
{
form=\u mapper.Map(formDto);
}
其他的
{
抛出新异常(“未知表单类型”);
}
_unitOfWork.FormRepository.AddFormMessage(表单);
}
您能出示您的dbContext吗?