使用实体框架CTP的C#的域建模问题4 [英] Domain Modeling Question using C# with Entity Framework CTP 4

查看:119
本文介绍了使用实体框架CTP的C#的域建模问题4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计出一张有照片集合的相册。每张专辑都将收录一张照片和一张拇指照片。这是我有的,但EF似乎不喜欢它:

I am trying to model out a album that has a collection of photos. Each Album will have a collection of Photos and a Photo that is a thumb. This is what I have but EF does not seem to like it:

public class Album : IEntity {
        private DateTime _dateCreated;

        public Album() {
            _dateCreated = SystemTime.Now();
            Photos = new List<Photo>();
        }
        public long Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public DateTime DateCreated
        {
            get { return _dateCreated; }
            set { _dateCreated = value; }
        }
        public virtual Site Site { get; set; }
        public virtual Photo Thumbnail { get; set; }
        public virtual ICollection<Photo> Photos { get; set; }
    }

public class Photo : IEntity {
            public Photo() {
                _dateCreated = SystemTime.Now();
            }
            private DateTime _dateCreated;
            public long Id { get; set; }
            public string Caption { get; set; }
            public string FileName { get; set; }
            public DateTime DateCreated
            {
                get { return _dateCreated; }
                set { _dateCreated = value; }
            }
            public virtual Album Album { get; set; }

        }

 public class AlbumMap : EntityConfiguration<Album>
    {
        public AlbumMap()
        {
            HasKey(x => x.Id);
            Property(x => x.Id).IsIdentity();
            Property(x => x.Location).IsVariableLength().HasMaxLength(80);
            Property(x => x.Name).IsVariableLength().HasMaxLength(80).IsRequired();
            Property(x => x.DateCreated);
            MapSingleType(a => new
            {
                a.Id,
                SiteId = a.Site.Id,
                ThumbnailId = a.Thumbnail.Id,
                a.Location,
                a.Name,
                a.DateCreated
            }).ToTable("Albums");
        }
    }

public class PhotoMap : EntityConfiguration<Photo>
    {
        public PhotoMap()
        {
            HasKey(x => x.Id);
            Property(x => x.Id).IsIdentity();
            Property(x => x.FileName).IsVariableLength().HasMaxLength(255).IsRequired();
            Property(x => x.Caption).IsVariableLength().HasMaxLength(255);
            Property(x => x.DateCreated);
            MapSingleType(a => new
            {
                a.Id,
                SiteAlbumId = a.Album.Id,
                a.FileName,
                a.Caption,
                a.DateCreated
            }).ToTable("AlbumPhotos");
        }
    }

我错过了什么,还是看起来不错?我期望EF在我的数据库中生成1到许多,但它不断创建专辑和照片(Album_Photos)之间的参考表,但这不应该是多对多的。任何帮助都会很好。

Am I missing something or does this look right? I am expecting EF to generate a 1 to many in my database but it keeps creating a reference table between Album and Photos (Album_Photos) but this should not be a many-to-many. Any help would be great.

推荐答案

按照惯例,EF代码首先执行创建链接表在典型的一对多场景中,您得到的原因是因为您的相册和照片对象之间的关联已被EF采用为许多到许多关联的一种:


每个相册有一张照片集合,每张照片都有一张相册集,这张照片是缩略图(虽然相关的导航属性没有在照片类中明确指定,只有相册具有缩略图属性)。

By convention, EF code first does not create a link table in typical one to many scenario, the reason that you are getting it is because your associations between Album and Photo objects has been taken by EF as being a kind of many to many association:
Each album has a collection of Photos and also each Photo has a collection of Albums that this photo is a thumbnail for (although the related navigation property is not explicitly specified on Photo class and only Album has a Thumbnail property).

截至EF CTP4,解决此问题的唯一方法是利用Fluent API,但在此之前,我修改模拟一点,并在您的模型中添加两​​个显式FK,以最大限度地灵活地处理您的对象。他们在上的 AlbumId 照片 ThumbnailId 相册

As of EF CTP4, the only way to fix this is by leveraging Fluent API, but before that, I modify your model a little bit and add two explicit FKs to your model to give you ultimate flexibility to work with your objects. They are AlbumId on Photo and ThumbnailId on Album:

public class Photo {        
    public long Id { get; set; }
    public string Caption { get; set; }
    public string FileName { get; set; }
    public DateTime DateCreated { get; set; }

    public long AlbumId { get; set; }
    public virtual Album Album { get; set; }
}

public class Album {
    public long Id { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public DateTime DateCreated { get; set; }

    public long ThumbnailId { get; set; }
    public virtual Photo Thumbnail { get; set; }

    public virtual ICollection<Photo> Photos { get; set; }
}

public class PhotoMap : EntityConfiguration<Photo> {
    public PhotoMap() 
    {
        this.HasRequired(p => p.Album)
            .WithMany(a => a.Photos)
            .HasConstraint((p, a) => p.AlbumId == a.Id);

        Property(x => x.FileName).IsVariableLength().HasMaxLength(255)
                                                    .IsRequired();
        Property(x => x.Caption).IsVariableLength().HasMaxLength(255);
        MapSingleType(p => new {
            p.Id,
            SiteAlbumId = p.AlbumId,
            p.FileName,
            p.Caption,
            p.DateCreated
        })
        .ToTable("Photo");
    }
}

public class AlbumMap : EntityConfiguration<Album> {
    public AlbumMap()
    {
        this.HasRequired(a => a.Thumbnail)
            .WithMany()
            .WillCascadeOnDelete(false)
            .HasConstraint((a, p) => p.Id == a.ThumbnailId);

        Property(x => x.Location).IsVariableLength().HasMaxLength(80);
        Property(x => x.Name).IsVariableLength().HasMaxLength(80).IsRequired();
        MapSingleType(a => new {
            a.Id,
            a.ThumbnailId,
            a.Location,
            a.Name,
            a.DateCreated
        })
        .ToTable("Album");
    }
}



结果是遵循所需的模式:


This results to the following desired schema:

这篇关于使用实体框架CTP的C#的域建模问题4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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