简体   繁体   中英

Entity Framework and One to Many Relationships Code First

I have been having trouble understanding what I would think is a simple relationship model. I have a CloudFile object that represents a file on a CDN. I have two other objects, Creative and Merchant , that relate to the CloudFile .

My CloudFile Class

public class CloudFile
{
    [Key]
    public int CloudFileId { get; set; }

    [StringLength(50)]
    public string OriginalFileName { get; set; }

    [StringLength(100)]
    public string Name { get; set; }

    [StringLength(5)]
    public string FileExt { get; set; }

    public int Height { get; set; }
    public int Width { get; set; }
    public long SizeInBytes { get; set; }
    public DateTime DateCreated { get; set; }

    public CloudFile()
    {
        this.DateCreated = DateTime.Now;
    }
}

My Creative Class

public class Creative
{
    [Key]
    public int CreativeId { get; set; }

    [Required]
    public int OfferId { get; set; }
    public virtual Offer Offer { get; set; }

    public virtual CloudFile CloudFile { get; set; }

    [StringLength(100)]
    public string Alt { get; set; }

    public DateTime DateCreated { get; set; }

    public Creative()
    {
        this.DateCreated = DateTime.Now;
        this.CloudFile = new CloudFile();
    }
}

My Merchant Class

public class Merchant
{
    #region Keys
    [Key]
    [Display(Name = "Merchant ID")]
    public int MerchantId { get; set; }
    #endregion

    [Required]
    [Display(Name = "Merchant Name")]
    [StringLength(256)]
    public string Name { get; set; }

    public virtual CloudFile CloudFile { get; set; }

    public ICollection<Offer> Offers { get; set; }
    public ICollection<MerchantLocation> Locations { get; set; }

    public Merchant()
    {
        this.Offers = new List<Offer>();
        this.Locations = new List<MerchantLocation>();
    }

I am not sure how to associate and save these objects properly in the database. I have tried adding a new CloudFile object to Merchant.CloudFile and then running the following:

    ......
    merchant.CloudFile = new CloudFile() {...set properties here...};
    MerchantDB.Modify(merchant);
    ......

    public static bool Modify(Merchant merchant)
    {
        using (AppDbContext db = new AppDbContext())
        {
            db.Entry(merchant).State = EntityState.Modified;

            //Return true if only 1 record was modified.  If not, return false.
            return (db.SaveChanges() == 1);
        }
    }

But it does not save the CloudFile and associate it with Merchant. I am able to directly save the CloudFile to the CloudFile table but there is still no association.

Thanks for you assistance in advance!

That is because you had not mentioned two way relationship , ie no navigation property from your cloudtfile to creative and merchant class , try adding below lines to your cloudfile model

public Creative creativeObj { get; set; }
public Merchant merchantObj { get; set; }

also change

db.Entry(merchant).State = EntityState.Modified; 

to

db.Entry(cloudfile).State = EntityState.Modified;

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