1
我是一个具有Ruby和Node.js背景的开发,但目前正在使用实体框架& SQL Server的C#API。从一个实体对象继承的对象放入数据库与铸
我有一个实体“Fon_Campaign”,但我想一些方法添加到该实体,所以我增加了一个对象“运动”,从“Fon_Campaign”继承,并给这个新对象的方法isValid()
该款腕表价值和如果对象是正确的,则回答。
我的API方法
[Route("Campaigns")]
[HttpPost]
public HttpResponseMessage CreateCampaigns([FromBody]Campaign campaign)
{
if (campaign == null)
{
ErrorMessage resp = new ErrorMessage();
resp.error = "Parameters are missing";
return Request.CreateResponse(HttpStatusCode.BadRequest, resp);
}
else if (campaign.IsValid())
{
Fon_Campaign c = campaign;
db.Fon_Campaign.Add(c); // Error: An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code, c is considering like an Campaign value and not Fon_Campaign
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, c);
}
else
{
ErrorMessage resp = new ErrorMessage();
// Add some explanation
resp.error = "New campaign is invalid";
return Request.CreateResponse(HttpStatusCode.BadRequest, resp);
}
}
当我试图保存对象到我的数据库,我通知这个对象是“运动”,我不能把它放到数据库,因为我必须把在Fon_Campaign
,所以演员没有工作。
我的模型
public class Campaign : Fon_Campaign
{
public bool IsValid()
{
return true;
}
}
希望能帮到你,或者给我一些指示为做一个最好的办法。