在对多个字段使用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

本文是小编为大家收集整理的关于在对多个字段使用AddOrUpdate匹配的ASP.NET MVC播种时,出现 "在此情况下只支持原始类型或枚举 "的错误。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

我正在尝试用一些测试数据播种一个开发数据库,​​需要在两个字段上匹配,并在此处遇到的问题date用EF 4.3

中的复杂键播种数据

我得到Only primitive types or enumeration types are supported in this context.错误 lukyer .

中提到

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

关于如何成功地收到有关如何成功播种数据库的任何想法.

模型的重要部分如下:

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;}
}

然后从配置迁移的代码添加了一个用户(jamesbond@mi5.com)和一个机构(最高机构机构).然后,它尝试在用户和机构之间添加关联,仅当两者之间的关联尚不存在时,才应添加它.

//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建议的建议,包括此处描述的所有解决方案我应该如何使用MVC3中的代码框架(4.1)声明外键关系?

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

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).然后需要更改该模型,因此它包括称为userId和InstitutionId的属性,可以通过Linq.

查询.

我按以下方式调整了UserInstitutionAssociation代码,因此它包括称为userId和InstitutionId的属性以及用户和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调用如下:

//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")
    }
);

本文地址:https://www.itbaoku.cn/post/1556924.html

问题描述

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

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

I get the Only primitive types or enumeration types are supported in this context. error as mentioned in the comments by 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.

The important parts of the model are as follows:

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;}
}

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();

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;}
}

and

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;}
}

推荐答案

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.

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;}
}

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")
    }
);