简体   繁体   English

一对一关系实体框架出错

[英]Error with one to one relationship entity framework

Good Day: 美好的一天:

I'm trying to accomplish one to one mapping using Entity Framework but, getting this error: 我正在尝试使用Entity Framework完成一对一的映射,但是出现此错误:

The foreign key component 'Id' is not a declared property on type 'UserUpload'. 外键组件“ Id”不是类型为“ UserUpload”的已声明属性。 Verify that it has not been explicitly excluded from the model and that it is a valid primitive property. 验证尚未将其显式排除在模型之外,并且它是有效的原始属性。

This is my code below: 这是我的代码如下:

 public abstract class Upload 
    {
        [Key]
        [Ignore]
        public virtual Guid Id { get; set; }

        public string Path { get; set; }

        public string Name { get; set; }

        public long   Size { get; set; }

        public UploadTypes Type { get; set; }

        [Ignore]
        [Timestamp]
        public byte[] RowVersion { get; set; }
    }

And my subclass: 和我的子类:

   public class UserUpload : Upload
    {
        [Key]
        [ForeignKey("User")]
        public override Guid Id { get { return base.Id; } set { base.Id = value; } }


        public ApplicationUser User { get; set; }

    }

In my abstract class, I have [Ignore] due to the fact that I want the property ignored by NEST (ElasticSearch dependency). 在我的抽象类中,由于我希望该属性被NEST(ElasticSearch依赖项) [Ignore] ,因此我具有[Ignore] I need this for Entity Framework nonetheless (my UserUpload ). 尽管如此,我仍然需要实体框架(我的UserUpload )。

Verify that it has not been explicitly excluded from the model 验证是否未将其明确排除在模型之外

[Ignore]
[Key]
public virtual Guid Id { get; set; }

You HAVE explicitly excluded it from the model. 已将其明确排除在模型之外。

I would suggest to add the key configuration in the modelbuilder for that model. 我建议在该模型的模型构建器中添加关键配置。 I found that writting there the model configuration is usually a better option. 我发现在那里写模型配置通常是一个更好的选择。

Finally, check: 'and that it is a valid primitive property.' 最后,检查:“并且它是有效的原始属性。”

Guid is not a primitive, use int instead. Guid不是原始类型,而是使用int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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