简体   繁体   中英

Auto-generated class from EF6 to implement custom interface

I am using EF 6. I have a table in db for which the auto-generated class looks like this:

public partial class tblPreparation
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public tblPreparation()
    {
        this.tblPreparationItem = new HashSet<tblPreparationItem>();
    }

    public int id { get; set; }
    public string name { get; set; }
    public System.DateTime date { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<tblPreparationItem> tblPreparationItem { get; set; }
}

In my code, I want this class to extend another class EntityObject, which is in the namespace System.Data.Entity.Core.Objects.DataClasses (and implement another interface). So I created wrote this partial class:

public partial class tblPreparation : EntityObject, IMyInterface
{

}

It doesn't throw a syntax error but when I run the application I get runtime error: "The type 'tblPreparation' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject." What am I missing?

I assum it's because they are not in the same namespace. Is there a way to fix this?

I may have misunderstood your comment on the namespace, but for clarity, a Partial class is only actually a partial class when it is in the same namespace of the corresponding partial class, otherwise what you have are just two separate single classes with the same name claiming to be partial. If this is the case, the fix is simple. Put them in same namespace.

However, it is more likely due to adding the EntityObject to the class hierarchy, as oerkelens mentioned. EF 6 creates proxies of your POCOs, for this reason your classes must have parameterless constructors. Adding another class may prevent the db context from creating proxies of your objects.

Remove just that class from the hierarchy, check whether you can materialise these entities to verify or rule it out.

Edit - No, it definitely is due to EntityObject .

I reproduced this by first having my entity implement some interface in a partial class. That worked great. Then I had partial class inherit from EntityObject that failed with your error.

After reproducing this error, I created a class called MyStupidClass and replaced EntityObject with MyStupidClass and I could still materialise entities (even with the top level properties of EntityObject ).

So it depends on the class you introduced to the hierarchy.

class Program
{
    static void Main(string[] args)
    {
        using (var db = new schedulerEntities())
        {
            var schedules = db.Schedules.ToArray();
            foreach (var schedule in schedules)
            {
                Console.WriteLine($"{schedule.Cron} - {schedule.FriendlyDescription}");
            }
        }
        Console.ReadLine();
    }

}
public partial class Schedule: MyStupidClass, IScheduler
{
    public string FirstName { get; set; }
}

public class MyStupidClass
{
    public EntityKey EntityKey { get; set; }
    public EntityState State { get; set; }
}

interface IScheduler
{
    long Id { get; set; }
    string Name { get; set; }
    string Cron { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM