当前位置: 移动技术网 > IT编程>开发语言>.net > (21)ASP.NET Core EF创建模型(关系)

(21)ASP.NET Core EF创建模型(关系)

2019年10月24日  | 移动技术网IT编程  | 我要评论

克里斯蒂安战记,陈浩明演的电视剧,学校风流魔君

1.关系

关系定义两个实体之间的关系。在关系型数据库中,这由外键约束表示。

2.术语定义

有许多术语用于描述关系:
●相关实体:这是包含外键属性的实体。有时称为关系的"子级"。
●主体实体:这是包含主/备用键属性的实体。有时称为关系的 "父项"。
●外键:依赖实体中的属性,用于存储与实体相关的主体键属性的值。
●主体密钥:唯一标识主体实体的属性。这可能是主键或备用密钥。
●导航属性:在主体和/或从属实体上定义的属性,该属性包含对相关实体的引用。
●集合导航属性:一个导航属性,其中包含对多个相关实体的引用。
●引用导航属性:保存对单个相关实体的引用的导航属性。
●反向导航属性:讨论特定导航属性时,此术语是指关系另一端的导航属性。
下面的代码列表显示了与之间blog的一对多关系post
●post是依赖实体
●blog是主体实体
●post.blogid为外键
●blog.blogid是主体键(在这种情况下是主键,而不是备用键)
●post.blog是一个引用导航属性
●blog.posts是集合导航属性
●post.blog是的blog.posts反向导航属性(反之亦然)

public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }
    public list<post> posts { get; set; }
}
public class post
{
    public int postid { get; set; }
    public string title { get; set; }
    public string content { get; set; }
    public int blogid { get; set; }
    public blog blog { get; set; }
}

3.约定

按照约定,当发现类型上有导航属性时,将创建关系。如果属性指向的类型不能由当前的数据库提供程序映射为标量类型,则该属性视为一个导航属性。

4.完全定义的关系

关系最常见的模式是在关系两端定义导航属性,在依赖实体类中定义外键属性。
如果在两个类型之间找到一对导航属性,则这些属性将配置为同一关系的反向导航属性。
如果依赖实体包含名为<primary key property name>、<navigation property name><primary key property name>或<principal entity name><primary key property name>的属性,则该属性将被配置为外键。

public class blog
{
    public int blogid { get; set; }
 public string url { get; set; }
    //导航属性
    public list<post> posts { get; set; }
}
public class post
{
    public int postid { get; set; }
    public string title { get; set; }
    public string content { get; set; }
    //外键属性
    public int blogid { get; set; }
    //反向导航属性
    public blog blog { get; set; }
}

5.无外键属性

尽管建议在依赖实体类中定义外键属性,但这并不是必需的。如果未找到外键属性,则会以该名称<navigation property name><principal key property name>引入阴影外键属性。

public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }
    //阴影导航属性
    public list<post> posts { get; set; }
}
public class post
{
    public int postid { get; set; }
    public string title { get; set; }
    public string content { get; set; }
     //阴影反向导航属性
    public blog blog { get; set; }
}

6.单个导航属性

只包含一个导航属性(无反向导航,没有外键属性)就足以具有约定定义的关系。 还可以有一个导航属性和一个外键属性。

public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }
    //阴影导航属性
    public list<post> posts { get; set; }
}
public class post
{
    public int postid { get; set; }
    public string title { get; set; }
    public string content { get; set; }
}

7.数据注释

可以使用两个数据批注来配置关系[foreignkey]和[inverseproperty]。system.componentmodel.dataannotations.schema命名空间中提供了这些项。

7.1foreignkey

你可以使用数据批注来配置应用程序作给定关系的外键属性的属性。通常,当不按约定发现外键属性时,会执行此操作。

namespace efmodeling.dataannotations.relationships.foreignkey
{
    class mycontext : dbcontext
    {
        public dbset<blog> blogs { get; set; }
        public dbset<post> posts { get; set; }
    }
    #region entities
    public class blog
    {
        public int blogid { get; set; }
        public string url { get; set; }
        //导航属性
        public list<post> posts { get; set; }
    }
    public class post
    {
        public int postid { get; set; }
        public string title { get; set; }
        public string content { get; set; }
        //外键
        public int blogforeignkey { get; set; }
        //设置反向导航外键
        [foreignkey("blogforeignkey")]
        public blog blog { get; set; }
    }
    #endregion
}

7.2inverseproperty

您可以使用数据批注来配置依赖项和主体实体上的导航属性如何配对。这通常在两个实体类型之间存在多个导航属性对时执行。

namespace efmodeling.dataannotations.relationships.inverseproperty
{
    class mycontext : dbcontext
    {
        public dbset<post> posts { get; set; }
        public dbset<user> users { get; set; }
    }
    #region entities
    public class post
    {
        public int postid { get; set; }
        public string title { get; set; }
        public string content { get; set; }

        public int authoruserid { get; set; }
        public user author { get; set; }

        public int contributoruserid { get; set; }
        public user contributor { get; set; }
    }
    public class user
    {
        public string userid { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        
        [inverseproperty("author")]
        public list<post> authoredposts { get; set; }
       
        [inverseproperty("contributor")]
        public list<post> contributedtoposts { get; set; }
    }
    #endregion
}

8.fluent api

若要在熟知的api中配置关系,请首先标识构成关系的导航属性。hasone或hasmany标识要开始配置的实体类型上的导航属性。然后,将调用链接到withone或withmany以标识反向导航。hasone/withone用于引用导航属性,hasmany / withmany用于集合导航属性。

namespace efmodeling.fluentapi.relationships.noforeignkey
{
    #region model
    class mycontext : dbcontext
    {
        public dbset<blog> blogs { get; set; }
        public dbset<post> posts { get; set; }
        protected override void onmodelcreating(modelbuilder modelbuilder)
        {
            modelbuilder.entity<post>()
                //配置一对多关系
                .hasone(p => p.blog)
                .withmany(b => b.posts);
        }
    }
    public class blog
    {
        public int blogid { get; set; }
        public string url { get; set; }
        public list<post> posts { get; set; }
    }
    public class post
    {
        public int postid { get; set; }
        public string title { get; set; }
        public string content { get; set; }
        public blog blog { get; set; }
    }
    #endregion
}

8.1单个导航属性

如果只有一个导航属性,则用withone、withmany的无参数重载。这表示在概念上,关系的另一端有一个引用或集合,但实体类中不包含导航属性。

namespace efmodeling.fluentapi.relationships.onenavigation
{
    #region model
    class mycontext : dbcontext
    {
        public dbset<blog> blogs { get; set; }
        public dbset<post> posts { get; set; }
        protected override void onmodelcreating(modelbuilder modelbuilder)
        {
            modelbuilder.entity<blog>()
                //配置多对一关系
                .hasmany(b => b.posts)
                .withone();
        }
    }
    public class blog
    {
        public int blogid { get; set; }
        public string url { get; set; }
        //导航属性
        public list<post> posts { get; set; }
    }
    public class post
    {
        public int postid { get; set; }
        public string title { get; set; }
        public string content { get; set; }
    }
    #endregion
}

8.2foreignkey

你可以使用api来配置应用程序的外键属性。

namespace efmodeling.configuring.dataannotations.samples.relationships.foreignkey
{
    #region model
    class mycontext : dbcontext
    {
        public dbset<blog> blogs { get; set; }
        public dbset<post> posts { get; set; }
        protected override void onmodelcreating(modelbuilder modelbuilder)
        {
            modelbuilder.entity<post>()
                //配置一对多关系
                .hasone(p => p.blog)
                .withmany(b => b.posts)
                //配置外键
                .hasforeignkey(p => p.blogforeignkey);
        }
    }
    public class blog
    {
        public int blogid { get; set; }
        public string url { get; set; }
        //导航属性
        public list<post> posts { get; set; }
    }
    public class post
    {
        public int postid { get; set; }
        public string title { get; set; }
        public string content { get; set; }
        //外键
        public int blogforeignkey { get; set; }
        public blog blog { get; set; }
    }
    #endregion
}

下面的代码列表演示如何配置复合外键:

namespace efmodeling.configuring.dataannotations.samples.relationships.compositeforeignkey
{
    #region model
    class mycontext : dbcontext
    {
        public dbset<car> cars { get; set; }
        protected override void onmodelcreating(modelbuilder modelbuilder)
        {
            modelbuilder.entity<car>()
                //配置复合主键
                .haskey(c => new { c.state, c.licenseplate });
                modelbuilder.entity<recordofsale>()
                //配置一对多关系
                .hasone(s => s.car)
                .withmany(c => c.salehistory)
                //配置外键
                .hasforeignkey(s => new { s.carstate, s.carlicenseplate });
        }
    }
    public class car
    {
        public string state { get; set; }
        public string licenseplate { get; set; }
        public string make { get; set; }
        public string model { get; set; }
        //导航属性
        public list<recordofsale> salehistory { get; set; }
    }
    public class recordofsale
    {
        public int recordofsaleid { get; set; }
        public datetime datesold { get; set; }
        public decimal price { get; set; }
        //state对应carstate
        public string carstate { get; set; }
        //licenseplate 对应carlicenseplate
        public string carlicenseplate { get; set; }
        public car car { get; set; }
    }
    #endregion
}

您可以使用的hasforeignkey(...)字符串重载将影子属性配置为外键。建议先将影子属性显式添加到模型,然后再将其用作外键:

class mycontext : dbcontext
{
    public dbset<blog> blogs { get; set; }
    public dbset<post> posts { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        // add the shadow property to the model
        modelbuilder.entity<post>()
             //配置外键
            .property<int>("blogforeignkey");
        // use the shadow property as a foreign key
        modelbuilder.entity<post>()
            //配置一对多关系
            .hasone(p => p.blog)
            .withmany(b => b.posts)
            //配置外键
            .hasforeignkey("blogforeignkey");
    }
}
public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }
    public list<post> posts { get; set; }
}
public class post
{
    public int postid { get; set; }
    public string title { get; set; }
    public string content { get; set; }
    public blog blog { get; set; }
}

8.3无导航属性

不一定需要提供导航属性。你可以直接在关系的一端提供外键。

namespace efmodeling.fluentapi.relationships.nonavigation
{
    #region model
    class mycontext : dbcontext
    {
        public dbset<blog> blogs { get; set; }
        public dbset<post> posts { get; set; }
        protected override void onmodelcreating(modelbuilder modelbuilder)
        {
            modelbuilder.entity<post>()
                //配置一对多关系
                .hasone<blog>()
                .withmany()
                //配置外键
                .hasforeignkey(p => p.blogid);
        }
    }
    public class blog
    {
        public int blogid { get; set; }
        public string url { get; set; }
    }
    public class post
    {
        public int postid { get; set; }
        public string title { get; set; }
        public string content { get; set; }
        public int blogid { get; set; }
    }
    #endregion
}

9.主体密钥

如果你希望外键引用主键之外的属性,则可以使用熟知的api来配置关系的主体键属性。 配置为主体密钥的属性将自动设置为备用密钥。

class mycontext : dbcontext
{
    public dbset<car> cars { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.entity<recordofsale>()
            .hasone(s => s.car)
            .withmany(c => c.salehistory)
            .hasforeignkey(s => s.carlicenseplate)
            .hasprincipalkey(c => c.licenseplate);
    }
}
public class car
{
    public int carid { get; set; }
    public string licenseplate { get; set; }
    public string make { get; set; }
    public string model { get; set; }

    public list<recordofsale> salehistory { get; set; }
}
public class recordofsale
{
    public int recordofsaleid { get; set; }
    public datetime datesold { get; set; }
    public decimal price { get; set; }

    public string carlicenseplate { get; set; }
    public car car { get; set; }
}

下面的代码列表演示如何配置复合主体键:

class mycontext : dbcontext
{
    public dbset<car> cars { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.entity<recordofsale>()
            .hasone(s => s.car)
            .withmany(c => c.salehistory)
            .hasforeignkey(s => new { s.carstate, s.carlicenseplate })
            .hasprincipalkey(c => new { c.state, c.licenseplate });
    }
}
public class car
{
    public int carid { get; set; }
    public string state { get; set; }
    public string licenseplate { get; set; }
    public string make { get; set; }
    public string model { get; set; }

    public list<recordofsale> salehistory { get; set; }
}
public class recordofsale
{
    public int recordofsaleid { get; set; }
    public datetime datesold { get; set; }
    public decimal price { get; set; }

    public string carstate { get; set; }
    public string carlicenseplate { get; set; }
    public car car { get; set; }
}

10.必需和可选的关系

您可以使用熟知的api来配置是必需的还是可选的关系。最终,这会控制外键属性是必需的还是可选的。当使用阴影状态外键时,这非常有用。如果实体类中具有外键属性,则关系的requiredness取决于外键属性是必需还是可选。

class mycontext : dbcontext
{
    public dbset<blog> blogs { get; set; }
    public dbset<post> posts { get; set; }

    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.entity<post>()
            .hasone(p => p.blog)
            .withmany(b => b.posts)
            .isrequired();
    }
}
public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }

    public list<post> posts { get; set; }
}
public class post
{
    public int postid { get; set; }
    public string title { get; set; }
    public string content { get; set; }

    public blog blog { get; set; }
}

11.级联删除

您可以使用熟知的api显式配置给定关系的级联删除行为。

class mycontext : dbcontext
{
    public dbset<blog> blogs { get; set; }
    public dbset<post> posts { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.entity<post>()
            .hasone(p => p.blog)
            .withmany(b => b.posts)
            .ondelete(deletebehavior.cascade);
    }
}
public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }

    public list<post> posts { get; set; }
}
public class post
{
    public int postid { get; set; }
    public string title { get; set; }
    public string content { get; set; }

    public int? blogid { get; set; }
    public blog blog { get; set; }
}

12.其他关系模式

12.1一对一

一对多关系在两侧都有一个引用导航属性。它们遵循与一对多关系相同的约定,但在外键属性上引入了唯一索引,以确保只有一个依赖项与每个主体相关。

12.1.1数据注释
public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }

    public blogimage blogimage { get; set; }
}
public class blogimage
{
    public int blogimageid { get; set; }
    public byte[] image { get; set; }
    public string caption { get; set; }

    public int blogid { get; set; }
    public blog blog { get; set; }
}
12.1.2fluent api

使用api 配置关系时,请使用hasone和withone方法。配置外键时,需要指定依赖实体类型,请注意以下列表hasforeignkey中提供的泛型参数。在一对多关系中,可以清楚地表明具有引用导航的实体是依赖项,并且具有集合的实体是主体。但这并不是一对一的关系,因此需要显式定义它。

class mycontext : dbcontext
{
    public dbset<blog> blogs { get; set; }
    public dbset<blogimage> blogimages { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.entity<blog>()
            .hasone(p => p.blogimage)
            .withone(i => i.blog)
            .hasforeignkey<blogimage>(b => b.blogforeignkey);
    }
}
public class blog
{
    public int blogid { get; set; }
    public string url { get; set; }

    public blogimage blogimage { get; set; }
}
public class blogimage
{
    public int blogimageid { get; set; }
    public byte[] image { get; set; }
    public string caption { get; set; }

    public int blogforeignkey { get; set; }
    public blog blog { get; set; }
}

12.2多对多

目前尚不支持多对多关系,没有实体类来表示联接表。但是,您可以通过包含联接表的实体类并映射两个不同的一对多关系,来表示多对多关系。

class mycontext : dbcontext
{
    public dbset<post> posts { get; set; }
    public dbset<tag> tags { get; set; }
    protected override void onmodelcreating(modelbuilder modelbuilder)
    {
        modelbuilder.entity<posttag>()
            .haskey(pt => new { pt.postid, pt.tagid });
        modelbuilder.entity<posttag>()
            .hasone(pt => pt.post)
            .withmany(p => p.posttags)
            .hasforeignkey(pt => pt.postid);
        modelbuilder.entity<posttag>()
            .hasone(pt => pt.tag)
            .withmany(t => t.posttags)
            .hasforeignkey(pt => pt.tagid);
    }
}
public class post
{
    public int postid { get; set; }
    public string title { get; set; }
    public string content { get; set; }

    public list<posttag> posttags { get; set; }
}
public class tag
{
    public string tagid { get; set; }

    public list<posttag> posttags { get; set; }
}
public class posttag
{
    public int postid { get; set; }
    public post post { get; set; }

    public string tagid { get; set; }
    public tag tag { get; set; }
}


参考文献:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网