[英]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: 我正在使用EF6。我在db中有一个表,为其自动生成的类如下所示:
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). 在我的代码中,我希望此类扩展另一个类EntityObject,该类位于名称空间System.Data.Entity.Core.Objects.DataClasses中(并实现另一个接口)。 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." 它不会引发语法错误,但在运行应用程序时会出现运行时错误:“未映射类型'tblPreparation'。请使用Ignore方法或NotMappedAttribute数据批注检查该类型是否未明确排除。该类型被定义为类,不是原始类型或泛型类型,并且不继承自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. 我可能会误解了您对名称空间的评论,但为清楚起见,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. 但是,更可能是由于将EntityObject添加到类层次结构中,如oerkelens所述。 EF 6 creates proxies of your POCOs, for this reason your classes must have parameterless constructors.
EF 6创建POCO的代理,因此,您的类必须具有无参数的构造函数。 Adding another class may prevent the db context from creating proxies of your objects.
添加另一个类可能会阻止db上下文创建对象的代理。
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
. 编辑 -不,这绝对是由于
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. 然后,我从
EntityObject
继承了部分类,但由于您的错误而失败。
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
). 重现此错误后,我创建了一个名为
MyStupidClass
的类,并用MyStupidClass
替换了EntityObject
,我仍然可以实现实体(即使具有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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.