我使用Entity Framework DB First 方法来创建我的模型。我花了很多时间与 EDMX 斗争以正确设置继承(0 个错误)。然后,当尝试使用 EF 创建带 View 的 MVC 5 Controller ,或为继承类添加 View 时,我收到此错误:
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.'
我有两个类:Person 和 Vendor。 Vendors 继承自 Persons,只添加一个名为 PackageID 的 int ID 字段。
如果有人知道为什么我会非常感激。到目前为止,EF 继承似乎比它的值(value)要麻烦得多。非常感谢。
编辑:也许我不应该为继承实体生成 View ?或者我没有正确配置继承(未将 base 设置为抽象?)
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; }
}
}
最佳答案
我找到了解决方案!好吧,至少让我为实体添加 Controller 和 View 。我也会为任何被困在这个问题上的人简要描述它,但是我计划在时间允许的情况下尽快回来并扩展这个答案。
碰巧脚手架在非继承实体上工作得很好,而且我的项目似乎不包含对 System.Core 的任何引用,更不用说重复的了。这让我认为这是继承和默认脚手架的兼容性问题。
修复步骤:
使用 NuGet 安装 MVCScaffolding。
PM> Install-Package MvcScaffolding
通过程序包管理器控制台运行以下命令
PM> Scaffold Controller Vendor -Repository
编辑:通过
-DbContextType [YourProjectEntities]
指定要更新的 DbContext 而不是它会创建一个您可能不想要的新对象。更新生成的 VendorRepository.cs 中的方法以使用 Persons 表而不是(不存在的)Vendors 表。以下是我的结果:
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(); } }
Eureka !
注意:参见 this blog用于标准用法、提示和自定义。
关于c# - 具有继承实体的 MVC 5 脚手架代码生成导致 System.Core 已导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22230677/