我有两个参考文献:
.Core
引用中我有一个具有业务功能的类。 我想创建一个简单的Crete
函数,但不能转换类型。
//my model in .Core:
public class A
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
public string phone { get; set; }
}
//my Business function in .Core:
public void Add(A a)
{
using (My_Entities context = new My_Entities())
{
context.tS.Add(a);
context.SaveChanges();
}
}
//My ViewModel:
public class AViewModel
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
public string phone { get; set; }
}enter code here
//My controller:
[HttpGet]
public ActionResult Add()
{
AViewModel d= new AViewModel ();
return PartialView("_Add", d);
}
[HttpPost]
public ActionResult Add(AViewModel a)
{
if (ModelState.IsValid)
{
ABusiness sb = new ABusiness ();
// sb.Add(a);
return RedirectToAction("List");
}
return PartialView("_Add", a);
}
答案 0 :(得分:1)
您需要存储数据库实体而不是业务对象。您可以在写入时将业务模型转换为实体模型。下面的示例。至少这是我假设你要做的事情。
public void Add(A a)
{
using (My_Entities context = new My_Entities())
{
context.tS.Add(new YourDatabaseEntity()
{
Id = a.id,
Name = a.name
// etc..
});
context.SaveChanges();
}
}