我将如何使用流利的Api进行映射 [英] How Would I Map This Using Fluent Api

查看:67
本文介绍了我将如何使用流利的Api进行映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们正在努力用流利的api来映射这个



这就是movies.cs



Hi guys im battling to to map this using fluent api

this is the movies.cs

[Key]
        [Column]
        public int MovieID { get; set; }
        [Column]
        public string Title { get; set; }
        [Column]
        public string Storyline { get; set; }
        [Column]
        public string Year { get; set; }
        [Column]
        public string Runtime { get; set; }
        [Column]
        public DateTime ReleaseDate { get; set; }
        [Column]
        public DateTime DateTimeAdded { get; set; }
        [Column]
        public DateTime? DateTimeEdited { get; set; }

public virtual ICollection<MovieProducers> MovieProducers { get; set; }





MovieProducers必须像这样maped





The MovieProducers must be maped like this

public int MovieProducerID { get; set; }
        public int MovieID { get; set; }
        public int PersonID { get; set; }

        public virtual People Person { get; set; }





我该怎么做:)



How would i do this :)

推荐答案

我'我猜你的意思是EF,使用流畅的API。



这是一个简单的模型,因此不需要自定义映射器。



I'm guessing that you mean EF, using fluent API.

This is a simple model, so shouldn't need a custom mapper.

public class Movie
{    
    [Key]
    public int MovieID { get; set; }
    public string Title { get; set; }
    public string Storyline { get; set; }
    public string Year { get; set; }
    public string Runtime { get; set; }
    public DateTime ReleaseDate { get; set; }
    public DateTime DateTimeAdded { get; set; }
    public DateTime? DateTimeEdited { get; set; }
 
    public virtual ICollection<MovieProducers> MovieProducers { get; set; }
}





然后我们通过简单地向MovieProducers添加外键属性来链接回来。此属性指向虚拟财产。





Then we link back by simply adding a foreign key attribute to MovieProducers. This attribute points to a virtual property.

public class MovieProducer
{
    [Key]
    public int MovieProducerID { get; set; }

    [ForeignKey("Movie")]
    public int MovieID { get; set; }

    [ForeignKey("Person")]
    public int PersonID { get; set; }
 
    public virtual Movie Movie{ get; set; }
    public virtual People Person { get; set; }
}





您应该使用此结构进行完全映射访问。



You should have fully mapped access with this structure.


我想我做对了



Movie.cs



I think i did it correct

Movie.cs

[Key]
        [Column]
        public int MovieID { get; set; }
        [Column]
        public string Title { get; set; }
        [Column]
        public string Storyline { get; set; }
        [Column]
        public string Year { get; set; }
        [Column]
        public string Runtime { get; set; }
        [Column]
        public DateTime ReleaseDate { get; set; }
        [Column]
        public DateTime DateTimeAdded { get; set; }
        [Column]
        public DateTime? DateTimeEdited { get; set; }

        public virtual ICollection<People> MovieProducers { get; set; }





People.cs





People.cs

public class People
    {
        [Key]
        [Column]
        public int PersonID { get; set; }
        [Column]
        public string FirstName { get; set; }
        [Column]
        public string MiddleName { get; set; }
        [Column]
        public string LastName { get; set; }

        public virtual ICollection<Movies> Movies { get; set; }
    }





DbContext.cs





DbContext.cs

public DbSet<Movies> Movies { get; set; }
        public DbSet<People> Person { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Movies>().
              HasMany(c => c.MovieProducers).
              WithMany(p => p.Movies).
              Map(
               m =>
               {
                   m.MapLeftKey("MovieID");
                   m.MapRightKey("PersonID");
                   m.ToTable("MovieProducer");
               });
        }


这篇关于我将如何使用流利的Api进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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