【问题标题】:Reusing a GUID in EF Code First DatabaseIntializer在 EF Code First DatabaseIntializer 中重用 GUID
【发布时间】: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


    【解决方案1】:

    在您的会员模型中,而不是存储 GUID“ApplicationId”来尝试和引用应用程序,您应该使用这样的导航属性 (see this link for better description of navigation properties):

    public class Memberships
    {
    
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid UserId { get; set; }
    
        //if you set your model up like this entity framework will take care of creating
        /the foreign key for you.
        public Application MemberApplication { get; set; }
    
    }
    

    然后将适当的应用程序传递给您的方法,如下所示:

    private static List<Memberships> addMemberships(Application app)
    {
        var mem = new List<Memberships>
        {
            new Memberships
            {
                UserId = Guid.NewGuid(),
                Application = app, 
            }
    
        };
        return mem;
    }
    

    这样设置模型可以让您充分利用 oop 和关系数据库。希望对您有所帮助。

    【讨论】:

    • 将这些传递到 Seed 方法的最佳方法是什么? ` protected override void Seed(DataContext context) { addApplications().ForEach(a => context.Applications.Add(a)); addUsers(Applications 应用程序).ForEach(u => context.User.Add(u)); }`它不喜欢这个lol
    • 用户是否获得了对特定应用程序的引用?还是可以有很多应用?
    • 现在它不支持它,但听起来不错,我必须有一个 UsersInApplications 类来支持它吗?
    • 要创建一个新问题,因为我认为在评论中需要解释很多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多