2

I created a simple solution with an EDMX file that possess one table Sport with 2 field IdSport and Label. I would like to insert a record in DB with an object inherited of the Sport object created by EF.

Public Class Index
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim aSport As New TestSport()
        Using ctx As New FormationEntities
            ctx.AddObject("Sport", aSport)
            ctx.SaveChanges()
        End Using
    End Sub
End Class

Public Class TestSport
    Inherits Sport
End Class

With an Sport object it work but not with TestSport. I need the inherited class for adding some properties and others functionnalities, but when I save it, I would like to save only the property possessed by the parent object Sport.

Error message:

Mapping and metadata information could not be found for EntityType

I know that the usual way is to use partial class but on my project, the EDMX file is in another project, so the only solution I see is to use an inherited class.

What am I doing wrong? How to fix my problem? Is it exist a better way to do it?

Thanks.

Arnaud Bessems
  • 515
  • 5
  • 21

2 Answers2

2

On searching through gooogle I found the following link, where a very similar scenario is discussed:

Deriving from classes generated by Entity Framework in C#

Although there is one post marked as answer, but the second answer is equally relevant.

Hope this helps.

Community
  • 1
  • 1
Shashank Chaturvedi
  • 2,756
  • 19
  • 29
1

Entity Framework appears to use a kind of reflection during the saving of your entities, and is probably why your inheritances do not work. One way you could still add functionality to your enties(albeit only functions) is using Extension methods: http://msdn.microsoft.com/en-us//library/bb383977.aspx

But if it is more than just some functions you need to add, consider a more structural solution. Having part of your object in a data layer and part of that same object in an upper layer is not a good separation of responsibilities.
Instead of having part of the class in your data project(I assume), and part of it in another project, consider creating one 'Logics class' in your project which wraps around your entity and adds functionality that way. You could for example do this by exposing the entity directly:

public class SportLogic  
{  
    private Sport _sport;  
    public Sport Sport { get { return _sport; } }  
    public string SomeNewProperty { get; set; }
    public void DoStuff() {};
}  

Or the way I use where the logics object is acting as an actual logical wrapper around the entity. It is cleaner because it obfuscates the entity entirely: any calling code wil not need knowledge of your entity(Sport) object.

public class SportLogic
{
    private Sport _sport;
    public string SportProperty { get { return _sport.SportProperty; } set { _sport.SportProperty = value; } }
    public string SomeNewProperty { get; set; }
    public void DoStuff() {};
}
Olaf
  • 879
  • 6
  • 19
  • My goal is to create a new architecture within an existing project, so exposing the entity Directly is the solution that suits me best. I will test how I can work like this. Do you have any other resources on a layered architecture based on Entity Framework? – Arnaud Bessems Mar 05 '14 at 07:57