3

I have used Entity Framework DB First approach to create my model. I spent many hours fighting with the EDMX to set up the inheritance correctly (0 errors). Then now when trying to create either an MVC 5 Controller w/ Views using EF, or add a View for the inheriting class I revieve this error:

There was an error running the selected code generator:

'There was an error in compilation of the type 'CostaPropertyServices.Models.PropertyInfoEntities'.

CS1703: An assembly with the same identity 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' has already been imported. Try removing one of the duplicate references.'

I have two classes: Person and Vendor. Vendors inherit from Persons, and add only an int ID field named PackageID.

EDMX Graphical representation of Person and Vendor entities

If anyone has any idea why I would really appreciate it. EF Inheritance seems to have been much more trouble than it's worth so far. Many Thanks.

Edit: Maybe I'm not supposed to be generating the views for inheriting entities? Or maybe I've not configured the inheritance correctly (Not set base to abstract?)


Person.cs

namespace CostaPropertyServices.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Person
    {
        public int PersonID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public Nullable<System.DateTime> Created { get; set; }
        public Nullable<System.DateTime> Modified { get; set; }
        public Nullable<System.DateTime> Deleted { get; set; }
    }
}

Vendor.cs

namespace CostaPropertyServices.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Vendor : Person
    {
        public int PackageID { get; set; }
    }
}
1
  • In my case the problem was that in solution I was referencing project that was compiling with .NET Framework 3.5. As soon as I've switched the project compilation to .NET Framework 4.5 everything started working.
    – nikib3ro
    Jan 15, 2015 at 20:14

1 Answer 1

2

I've found a solution! Well, to let me add controllers and views for the entity at least. I'll describe it briefly for anyone who is stuck on this too, however I plan to come back and expand this answer as soon as time allows.

It so happens that scaffolding worked completely fine with non-inheriting entities, and my project didn't seem to contain any references to System.Core, let alone a duplicate one. This made me think it was a compatibility issue with inheritance and the default scaffold-er.

Steps to Fix:

  1. Install MVCScaffolding using NuGet.

    PM> Install-Package MvcScaffolding
    
  2. Run the following command via Package Manager Console

    PM> Scaffold Controller Vendor -Repository
    

    Edit: Pass -DbContextType [YourProjectEntities] to specify a DbContext to update rather than it creating a new one you probably don't want.

  3. Update methods in generated VendorRepository.cs to use Persons table and not (non-existant) Vendors table. Below was my result:

    public class VendorRepository : IVendorRepository
    {
        PropertyInfoEntities context = new PropertyInfoEntities();
    
        public IQueryable<Vendor> All
        {
            get { return context.Persons.OfType<Vendor>(); }
        }
    
        public IQueryable<Vendor> AllIncluding(params Expression<Func<Vendor, object>>[] includeProperties)
        {
            IQueryable<Vendor> query = context.Persons.OfType<Vendor>();
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }
    
        public Vendor Find(int id)
        {
            return context.Persons.OfType<Vendor>().SingleOrDefault(v => v.PersonID == id);
        }
    
        public void InsertOrUpdate(Vendor vendor)
        {
            if (vendor.PersonID == default(int)) {
                // New entity
                context.Persons.Add(vendor);
            } else {
                // Existing entity
                context.Entry(vendor).State = System.Data.Entity.EntityState.Modified;
            }
        }
    
        public void Delete(int id)
        {
            var vendor = context.Persons.OfType<Vendor>().SingleOrDefault(v => v.PersonID == id);
            context.Persons.Remove(vendor);
        }
    
        public void Save()
        {
            context.SaveChanges();
        }
    
        public void Dispose() 
        {
            context.Dispose();
        }
    }
    
  4. Eureka!

NOTE: See this blog for standard usage, tips and customisation.

3
  • Your solution doesn't really work in my scenario: VS2012, MVC4 project, EF 5.0. I create the model first and follow this tutorial to add a "Discriminator" in my Person table: msdn.microsoft.com/en-us/data/jj618292.aspx I use the MvcScaffolding you suggested... it creates all views etc but it creates a new context and when I save something, it saves it in a new database. If I modify the context to be a partial class and be the same as the already existing context, I get the error "Could not determine storage version; a valid storage connection or a version hint is required."
    – firepol
    Jun 13, 2014 at 7:34
  • 1
    @firepol When using DB First, you need to pass '-DbContextType PropertyInfoEntities' as well to tell it to use your original model and not create a new one. This tool may have been designed with Code First in mind, however it's extremely powerful and I am using it to pre- and re-generate templates for all my entities. Jun 13, 2014 at 10:48
  • Wow, thanks! I did as you suggested and left away "Repository" (I don't need any repository). It worked like a charm except I have all my tables and controller "singular" but the tool pluralized e.g. controller, views "Students" instead of "Student"... I researched a bit and found out that this can be fixed by using this command: Scaffold Controller StudentController -DbContextType MyEntities (Changed in the initial command: "Controller StudentController", instead of "Controller Student", in my case)
    – firepol
    Jun 13, 2014 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.