none
problem on custom class inherit from model class that generate from EF DataBase First RRS feed

  • Question

  • Hi,Sorry for my poor English.

    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;}
    }

    a class in WEBAPI Model inherit the class above.

    public class MessageModel:Message
    {
    }

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

    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

    can you help me.


    ...

    Thursday, April 26, 2018 2:17 AM

All replies

  • The EF metadata is defined for  public partial class Message and EF knows about the object, because it's an object in the virtual object model.

    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.

    And then you try to add MessageModel to EF database context that has no DBSet<MessageModel> that it knows about. It has a DBset<Message> that it knows about. So MessageModel can't be mapped to anything.

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

    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.    

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

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

    Thursday, April 26, 2018 3:26 AM
  • Hi Chivas_Tan,

    Thank you for posting here.

    For your question is more related to EF, I will move it to ADO.NET Entity Framework and LINQ to Entities

    forum for suitable support.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 30, 2018 1:45 AM
  • Hi Chivas_Tan,

    According to your description, if you want to create other property, which avoid to clear through upload model, I would suggest that you could create partial class to achieve, create a class file, then change the name same as your message class, entity framework will combine them and map to the sample table, like below.

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

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 30, 2018 6:34 AM
    Moderator
  • In my solution, there are several projects that need to read and write to the same database, so I built a separate class library project Project A to read and write a database .There a message table in the project A ,and EF generated database object Messagemodel. a static class Operater to provide static methods to read and write the database, methods  need to pass a Messagemodel object.

    Other projects call the Operater class's method to read and write the table. These items need to be constrained by attribute to the class members. Different project has different constraints, so I can only generate new classes in other classes by inheritance. The attribute of the new class member is then bound. The new class is then passed to the Operater method. But doing so will give an error.

    ---
    看你名字像是用中文的.我能用中文答复么?

    我知道可以通过EF生成的类是一个partial 类,可以用另外一个partial 类来为这个类添加元数据描述.

    在我的solution中,有多个project需要读写同一个数据库,所以我单独建了一个类库项目project A用来读写数据库,数据库中有一个Message表,在project A中通过EF生成了数据库对象MessageModel,其中有一个静态类operater来提供静态方法读写数据库,方法需要传入MessageModel对象,其他的project调用operater类的方法读写该表.这些项目需要对类成员通过attribute来进行约束.不同的project有不同的约束,所以我只能在其他的类中通过继承的方式产生新类,然后在新类成员的attribute上进行约束.然后将新类传递给operater的方法.但是这样做就会报错.


    ...

    Wednesday, May 2, 2018 9:40 AM
  • In my opinion, you should have had an abstraction layer away from the EF virtual model and underlying database technology, which would be achievable by using the Repository pattern and the DTO pattern with the DTO(s) being used as  the abstraction.

    With the DTO(s), you could achieve what you are trying to without directly affecting the EF model. The model changes, then  any client would not be concerned with it, because the service technology, WebAPI is not directly working the model. The Repository  using the DTO(s) and mapping DTO to EF model and vice versa is doing the data persistence using EF. 

    Wednesday, May 2, 2018 11:22 AM