【发布时间】:2013-01-24 01:19:02
【问题描述】:
所以我正在尝试使用 EF 构建自定义成员资格。我真的不知道我在做什么,但到目前为止进展相当顺利。
我在数据库初始化步骤中,我试图在项目运行后立即将数据转储到数据库中。我的班级Application.cs 使用 GUID 作为主键,如下所示。我试图弄清楚如何将该 GUID 添加到数据库中。
我不知道这是否可能,但这就是我想要做的。当您在 VS 2012 中创建一个普通的 Web 应用程序项目并尝试使用 EF Code First 重新创建该数据库时,我使用了默认登录的数据库(为了练习)。这就是我到目前为止所得到的。
类
public class Applications
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ApplicationId { get; set; }
[StringLength(125)]
public string ApplicationName { get; set; }
}
在构建时将数据转储到数据库中的初始化程序(不包括种子。)
private static List<Applications> addApplications()
{
var apps = new List<Applications>
{
new Applications
{
ApplicationId = Guid.NewGuid(),
ApplicationName = "Test Login"
}
};
return apps;
}
private static List<Memberships> addMemberships()
{
var mem = new List<Memberships>
{
new Memberships
{
UserId = Guid.NewGuid(),
ApplicationId = ?, // How can this guid Match the one in
// in ApplicationId for the Application Table?
}
};
return mem;
}
我得到“Invalid Initializer member declarator”。我面临的问题是我需要在多个表中ApplicationId 的 GUIDS 相同。我什至不知道这是可能的还是正确的?
我觉得我必须以某种方式分享它,也许就像
Guid AppId;
AppId = Guid.NewGuid();
【问题讨论】:
标签: c# asp.net entity-framework ef-code-first