简体   繁体   中英

One To Many relationship entity framework without Code First Model Creation

EDIT: (simplified solution) I'm t rying to insert a Picture Entity in an Ad Entity that can have N Pictures. The Pictures are related with Ads.

Ad Model:

public class  Ad
{
    public int Id { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Picture> Pictures { get; set; }
}

Picture Model:

public class Picture
{
    public int Id { get; set; }
    public string URL { get; set; }

    public int Ad_Id { get; set; }
    public virtual Ad Ad { get; set; }

    public int PictureType_Id { get; set; }
    public virtual PictureType PictureType { get; set; }
}

PictureType Model:

public class PictureType
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Ad Service:

Picture picture = new Picture()
{
    Ad_Id = adId,
    Filename = newFileName,
    PictureType_Id = pictureType.Id
};

_pictureService.CreatePicture(picture);

Picture Service:

public void CreatePicture(Picture picture)
{
    _pictureRepository.Add(picture);
    _pictureRepository.Save();
}

ERROR: The query generated by this code is:

Execute Reader "insert [dbo].[Pictures]([Name], [Filename], [URL], [PictureType_Id], [Ad_Id], [PictureType_Id1], [Ad_Id1]) values (null, @0, null, @1, @2, null, null)"

And I Get the ERROR:

Thrown: "Invalid column name 'PictureType_Id1'. Invalid column name 'Ad_Id1'." (System.Data.SqlClient.SqlException) Exception Message = "Invalid column name 'PictureType_Id1'.\\r\\nInvalid column name 'Ad_Id1'. Exception Type = "System.Data.SqlClient.SqlException", Exception WinRT Data = ""

You are missing a PK/FK relationship between Ad and Picture. The default would be Ad_Id

public class Picture
{
    public int Id { get; set; }
    public string URL { get; set; }

    public int Ad_Id { get; set; }
    public virtual Ad Ad { get; set; }
}

The same is needed in your database.

And a relationship needs to be defined between the entities (something like this)

this.HasMany(c => c.AdPictures)
    .WithRequired(p => p.Ad)
    .HasForeignKey(k => k.Ad_Id);

Based on the additional information the problem appears to be:

Ad has a relationship with an intermediate table AdPicture but Picture has a direct relationship to Ad where as it should also have a relationship with the intermediate table AdPicture ?

this:

public class Picture
{
    public int Id { get; set; }
    public string URL { get; set; }

    public virtual AdPicture AdPicture { get; set; }
}

or this:

public class Picture
{
    public int Id { get; set; }
    public string URL { get; set; }

    public virtual ICollection<AdPicture> AdPictures { get; set; }
}

Is this make sense? :

Changes in Picture Model:

public class Picture
{
    // Primary properties
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Filename { get; set; }
    public string URL { get; set; }

    // Navigation properties
    //public int PictureType_Id { get; set; }
    public virtual PictureType PictureType { get; set; }

    //public int Ad_Id { get; set; }
    public virtual Ad Ad { get; set; }
}

Change in Ad Service:

// Insert Picture 
Picture picture = new Picture()
{
    Ad = _adRepository.GetById(adId),
    Filename = newFileName,
    PictureType = _pictureTypeService.GetPictureTypeById(pictureType.Id)
};

_pictureService.CreatePicture(picture);

Everything else remain the same.

Now it's working but it does not seem to be the best solution because I have 2 roundtrips to the database to get the Ad entity and the PictureType entity.

Am I right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM