在重用EF code首先DatabaseIntializer一个GUID [英] Reusing a GUID in EF Code First DatabaseIntializer

查看:160
本文介绍了在重用EF code首先DatabaseIntializer一个GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用EF来构建自定义的成员。我真的不知道我在做什么,但它已经比较平稳为止。

我在数据库初始化程序步骤,其中我试图为项目运行数据转储很快到数据库中。我班 Application.cs 使用GUID作为主键,如下图所示。我试图找出如何我可以添加GUID到数据库中。

我不知道这是可能的,但是这是我努力做的事。我把你的默认登录的数据库,当您在VS 2012一个正常的Web应用程序项目,并试图重新创建数据库使用EF code首先(作为练习)。这是我走到这一步。

 公共类应用
        {                [DatabaseGenerated(DatabaseGeneratedOption.Identity)
                公众的Guid的applicationID {搞定;组; }                [StringLength(125)]
                公共字符串应用程序名称{搞定;组; }        }

初​​始化器转储数据插入构建分贝(不包括种子)。

 私有静态列表<应用> addApplications()
    {
        VAR应用=新的List<应用>
        {
            新应用
            {
                的applicationID = Guid.NewGuid(),
                应用程序名称=测试登录            }
        };        返回应用程序;
    }
  私人静态列表<&成员GT; addMemberships()
    {
        VAR纪念品=新的List<&成员GT;
        {
            新成员
            {
                用户ID = Guid.NewGuid()
                的applicationID =?,//这怎么GUID匹配之一
                                   //中的applicationID的应用程序表?
            }        };
        返回MEM;
    }

我得到无效的初始化函数成员声明。我面临的问题是我需要的GUID来跨多个表是的applicationID 相同。我甚至不知道这是可能的还是右边?

我有一种感觉,我必须以某种方式也许分享它像

 的Guid的AppId;
        的AppId = Guid.NewGuid();


解决方案

在您的会员制模式,而不是存储GUID的applicationID,试图引用您应该使用导航属性,像这样的应用程序(<一个href=\"http://blog.staticvoid.co.nz/2012/7/17/entity_framework-navigation_property_basics_with_$c$c_first\"相对=nofollow>看到这个链接导航属性)的更好的描述:

 公共类成员
{    [DatabaseGenerated(DatabaseGeneratedOption.Identity)
    公众的Guid用户ID {搞定;组; }    //如果你设置你的模型像这样的实体框架会照顾创造
    /你的外键。
    公共应用MemberApplication {搞定;组; }}

然后就在相应的应用程​​序传递给你的方法,像这样:

 私有静态列表&LT;&成员GT; addMemberships(应用程序)
{
    VAR纪念品=新的List&LT;&成员GT;
    {
        新成员
        {
            用户ID = Guid.NewGuid()
            应用=应用程序,
        }    };
    返回MEM;
}

设置你的模型像这样可以让你充分利用OOP和关系数据库。希望有所帮助。

So I am trying to build Custom membership using EF. I dont really know what i am doing but it has gone fairly smooth so far.

I am on the Database Initializer step where i am trying to dump data into the database soon as the project runs. My class Application.cs uses a GUID as a primary key as seen below. I am trying to figure out how i can add that GUID into the database.

I don't know if this is possible but this is what i am trying to do. I took the default login's Database you get when you make a normal web application project in VS 2012 and trying to recreate that database using EF Code First(For practice). This is what i got so far.

Class

public class Applications
        {

                [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
                public Guid ApplicationId { get; set; }

                [StringLength(125)]
                public string ApplicationName { get; set; }

        }

Intializer to dump data into db on build(not including Seed.)

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

I get "Invalid Initializer member declarator". The problem i face is that I need the GUIDS to be the same for ApplicationId across multiple tables. I don't even know if this is possible or right?

I got a feeling I have to share it somehow maybe like

   Guid AppId;
        AppId = Guid.NewGuid();

解决方案

In your Membership model instead of storing the GUID "ApplicationId" to try and reference the application you should use a navigation property like so (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; }

}

then just pass in the appropriate application to your method like so:

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

    };
    return mem;
}

Setting your model up like this lets you take full advantage of oop and relational database. Hope that helps.

这篇关于在重用EF code首先DatabaseIntializer一个GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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