0

When trying to call SaveChanges(), I get the following error:

An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.'

SqlException: Invalid column name 'Artwork_Id'

I am using Entity Framework.

I'm trying to add an artworkImage that has the Id of an artwork as a reference. All information is being passed correctly but it's not saving.

I've tried adding foreign keys to my models and dbcontext but I've not gotten further than the code below.

public partial class ArtworkImage
{
    [Key]
    public int Id { get; set; }
    public string ImageURL { get; set; }
    public Artwork Artwork { get; set; }
}

public partial class Artwork
{
    [Key]
    public int Id { get; set; }
    public string Category { get; set; }
    public ICollection<ArtworkImage> ArtworkImage { get; set; }
}

My DbContext:

public DbContext()
        : base("name=DbConnection")
    {
        this.Configuration.AutoDetectChangesEnabled = false;
    }

    public virtual DbSet<Artwork> Artworks { get; set; }

    public virtual DbSet<ArtworkImage> ArtworkImages { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Artwork>()
            .Property(e => e.Category)
            .IsFixedLength();

        modelBuilder.Entity<Artwork>()
            .HasKey(b => b.Id);

        modelBuilder.Entity<ArtworkImage>()
            .HasKey(b => b.Id);

        Database.SetInitializer<DbContext>(null);
        base.OnModelCreating(modelBuilder);
    }

I believe I should be adding something like this to my dbcontext but I haven't quite figured it out yet.

            modelBuilder.Entity<ArtworkImage>()
            .HasRequired(p => p.Artwork)
            .WithMany(d => d.ArtworkImage)
            .HasForeignKey(p => p.Artwork);

If any information is missing please point it out and I'll add it.

DaSernet
  • 25
  • 5

2 Answers2

0

You have to declare primary key on each table. it is a rare occasion when a table has no PK. almost never.

[Key]
public int Id { get; set; }
Boppity Bop
  • 9,613
  • 13
  • 72
  • 151
  • I've added this to my code but it does not solve my issue. However in hindsight it was smart to add. – DaSernet Sep 02 '22 at 14:34
  • ok. nw. i guess you need to read docs since you just wading in the dark: https://learn.microsoft.com/en-us/ef/ef6/fundamentals/relationships – Boppity Bop Sep 02 '22 at 14:38
0

So part of your problem might be that you don't define the relationship in reverse which I believe is important in how it establishes if the relationship is one-to-one or one-to-many. So you will likely need to add a property on the Artwork class that is of type ArtworkImage (if it is one-to-one). if it is one-to-many you will need to make the property some generic collection with the generic of type ArtworkImage.

One-to-one

public partial class Artwork
{
    [Key]
    public int Id { get; set; }
    public string Category { get; set; }
    public ArtworkImage ArtworkImage { get; set; }
}

One-to-many

public partial class Artwork
{
    [Key]
    public int Id { get; set; }
    public string Category { get; set; }
    public IEnumerable<ArtworkImage> ArtworkImages { get; set; }
}
  • I had already tried something like this (one-to-many). public ICollection ArtworkImage { get; set; } To be more specific, IEnumerable throws an type error, and ICollection still throws the same error I was already getting. I believe I'm missing something in my DbContext and was missing the ICollection, so thanks for affirming that! – DaSernet Sep 02 '22 at 15:36