在这种情况下,“仅支持原始类型或枚举"错误,同时在多个字段上使用AddOrUpdate匹配为ASP.NET MVC注入种子 [英] `Only primitive types or enumeration are supported in this context` error whilst seeding ASP.NET MVC using AddOrUpdate match on multiple fields

查看:58
本文介绍了在这种情况下,“仅支持原始类型或枚举"错误,同时在多个字段上使用AddOrUpdate匹配为ASP.NET MVC注入种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在stackoverflow.com上的第一个问题!祝我好运解释:)

My first question on stackoverflow.com! Wish me luck explaining :)

我试图用一些测试数据为开发数据库播种,这需要在两个字段上进行匹配,并且在这里遇到了解决的问题-data-with-addorupdate-with-a-complex-key-in-ef-4-3>如何在EF 4.3中使用带有复杂键的AddOrUpdate播种数据

I am trying to seed a development database with some test data, requiring matching on two fields, and encountered the issue solved here How to seed data with AddOrUpdate with a complex key in EF 4.3

我收到Only primitive types or enumeration types are supported in this context.错误在lukyer的评论中提到.

但是,尽管按照他的建议添加了外键属性和模型属性中的必需属性,但我找不到明显的方法来清除错误.

However, despite adding foreign key attributes, and required attributes to the properties in the model as he suggested, I can find no obvious way to clear the error.

对于如何成功播种数据库的任何想法,将不胜感激.

Any ideas on how to seed the database successfully would be gratefully received.

模型的重要部分如下:

public class User {
    public string ID { get; set; }
    public string Email {get; set; }
}

public class Institution {
    public Guid ID { get; set;}
    public string Name {get; set; }
}

public class UserInstitutionAssociation {
    public Guid ID { get; set; }
    public virtual User User { get; set; }
    public virtual Institution Institution {get; set;}
}

然后从configuration.cs迁移的代码添加了一个用户(jamesbond@mi5.com)和一个机构(最高机密机构).然后,它尝试在用户和场所之间添加关联,只有在两者之间不存在关联的情况下,才应为其添加关联.

And then the code to migrate from configuration.cs adds a user (jamesbond@mi5.com) and an institution (Top Secret Establishment). It then attempts to add an association between the user and the establishment, for which it should be added only if an association between these two does not already exist.

//Add User to Database
context.Users.AddOrUpdate(u => u.Email,
    new User {
        Email = "jamesbond@mi5.com"
    });
context.SaveContext();

//Add Institution to Database
context.Institutions.AddOrUpdate(u => u.Name,
    new Institution {
        Name = "Top Secret Establishment"
    });
context.SaveContext();

//Add Association between User and Institution
context.InstitutionUserAssociations.AddOrUpdate(u => new { u.User, u.Institution },
    new InstitutionUserAssociation
    {
        User = context.Users.Single(x => x.Email=="jamesbond@mi5.com"),
        Institution = context.Institutions.Single(x => x.Name=="Top Secret Establishment")
    }
);
context.SaveContext();

据我了解Lukyer的建议,我尝试了多种方式更改模型,包括此处所述的所有解决方案

I have tried changing the model in a number of ways as I understand Lukyer to have suggested, including all solutions described here How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3?

简而言之,我尝试了以下所有组合,但均未成功:

In short, I have tried all combinations of the following without success:

public class UserInstitutionAssociation {
    public Guid ID { get; set; }
    [Required]
    [ForeignKey("UserID")]
    public virtual User User { get; set; }
    [Required]
    [ForeignKey("InstitutionID")]
    public virtual Institution Institution {get; set;}
}

public class UserInstitutionAssociation {
    public Guid ID { get; set; }
    [Required]
    [ForeignKey("User")]
    public string UserID {get; set; }
    public virtual User User { get; set; }

    [Required]
    [ForeignKey("Institution")]
    public Guid InstitutionID { get; set; }
    public virtual Institution Institution {get; set;}
}

推荐答案

已解决.由于发生在LINQ查询中的类而发生.您不能使用x => new { x.User, x.Institution},因为x.User和x.Institution都是复杂的类.相反,这些必须是原始基本类型(例如字符串或Guid).以此方式更改了AddOrUpdate(x => new {x.UserID, x.InstitutionID}, Associations).然后需要更改模型,使其包含LINQ可以查询的名为UserID和InstitutionID的属性.

Solved. Occurred because of the classes that I included as part of the LINQ query. You can't use x => new { x.User, x.Institution} because x.User and x.Institution are both complex classes. Instead, these need to be primitive base types (such as string or Guid). It was changed in this way AddOrUpdate(x => new {x.UserID, x.InstitutionID}, Associations). The model then needed to be changed so it included properties called UserID and InstitutionID that could be queried by LINQ.

我按如下所示调整了UserInstitutionAssociation代码,因此它包括称为UserID和InstitutionID的属性以及User和I:

I adjusted the UserInstitutionAssociation code as follows so it included properties called UserID and InstitutionID as well as the User and I:

public class UserInstitutionAssociation {
    public Guid ID { get; set; }

    [Required]
    [ForeignKey("User")]
    public string UserID {get; set; }
    public virtual User User { get; set; }

    [Required]
    [ForeignKey("Institution")]
    public Guid InstitutionID { get; set; }
    public virtual Institution Institution {get; set;}
}

然后按如下所示调整AddOrUpdate调用:

Then adjust the AddOrUpdate call as follows:

//Add Association between User and Institution
context.InstitutionUserAssociations.AddOrUpdate(u => new { u.UserID, u.InstitutionID },
    new InstitutionUserAssociation
    {
        User = context.Users.Single(x => x.Email=="jamesbond@mi5.com"),
        Institution = context.Institutions.Single(x => x.Name=="Top Secret Establishment")
    }
);

这篇关于在这种情况下,“仅支持原始类型或枚举"错误,同时在多个字段上使用AddOrUpdate匹配为ASP.NET MVC注入种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆