自定义类的问题继承自从EF DataBase First生成的模型类 [英] problem on custom class inherit from model class that generate from EF DataBase First

查看:82
本文介绍了自定义类的问题继承自从EF DataBase First生成的模型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,因为我的英语不好。

Hi,Sorry for my poor English.

我建立了一个WEBAPI项目。控制器中的一个Action将写入DataBase。我使用Entity Framework到SQL Server。 EF 生成模型

I build a WEBAPI Project. one Action in controller is going to write DataBase. I use Entity Framework to SQL Server. EF generate a Model

public partial class Message 
{
    public MsgId {get;set;}
}

WEBAPI模型中的类继承上面的类。

a class in WEBAPI Model inherit the class above.

public class MessageModel:Message
{
}

以这种方式,如果EF中的课程更新了,我不需要重写我的课程。

in this way,if the class in EF updated, I do not need to rewrite my class.

WEBAPI Controll中的操作在下面

a action in WEBAPI Controll is below

[Route()]
        [HttpPost]
        public HttpResponseMessage SaveMsg(MessageModel msg)
        {
            if (ModelState.IsValid)
            {
                try
                {
                     
                    DBEntities dbEntites = new DBEntities();
                    dbEntites.Messages.Add(msg);
                     
                    return this.Request.CreateResponse(new ApiResult { message = string.Empty });
                }
                catch (Exception ex)
                {
                    return this.Request.CreateResponse(new ApiResult { message = ex.Message });
                }
            }
            else
 
            { return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); }
 
 
        }

当我向此行动发送数据时。我有一个例外:

when i send data to this action.I got a exception:

 Mapping and metadata information could not be found for EntityType

你能帮忙吗?我。

...

推荐答案

EF元数据定义为  public
partial class
消息和EF知道对象,因为它是虚拟对象中的对象模型。

EF元数据没有定义  public
class MessageModel和EF不了解对象,因为它不是EF虚拟对象模型的一部分,它可以映射它。

The EF metadata has no definition  public class MessageModel, and EF doesn't know about the object, because it's not part of the EF virtual object model, and it can map it.

然后你尝试添加 MessageModel>它知道的。它有一个DBset< Message>它知道的。所以MessageModel无法映射到任何东西。

DBEntities dbEntites =
new DBEntities ();

         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; dbEntites
消息 添加 msg );

DBEntities dbEntites = new DBEntities();
                    dbEntites
.Messages.Add(msg);

在尝试将ORM使用的数据持久性对象应用于继承时,您在尝试使用继承做什么是有点可疑的。 &NBSP;  

It's kind of questionable as to what you are trying top do here with inheritance in trying to apply it a data persistence object used by the ORM.    

但是,您可以发布到EF论坛寻求帮助。

However, you can post to the EF forum for help.

https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework


这篇关于自定义类的问题继承自从EF DataBase First生成的模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆