0

Im using EF to update my database.

This is my model:

    //Resource
    public long ResourceId { get; set; }
    public string Name { get; set; }
    public string Descritption { get; set; }
    public string UserId { get; set; }
    public int ResourceTypeId { get; set; }
    public byte[] Data { get; set; }
    public bool IsEnabled { get; set; }
    public bool Approved { get; set; }
    public System.DateTime DateCreated { get; set; }
    public virtual ICollection<MetaData> MetaDatas { get; set; }


    //MetaData
    public long MetaDataId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string LanguageCode { get; set; }
    public string UserId { get; set; }
    public System.DateTime DateCreated { get; set; }
    public virtual ICollection<ResourceMetaList> ResourceMetaLists { get; set; }
    public virtual ICollection<Resource> Resources { get; set; }

What is it that makes metadataid increase by 1 everytime i add a new metadata? I want to be able to add metadata with a specific id?

There is a connectiontabl between resource and metadata where the id of each of them connnects them. so i want to be able to add a specific metadata for each resource but for now it gives it a new id everytime so it is not realy the same metadata that connects to each resource but a new one every time.

Edit

Maybe the problem is not how i modeled my database but how i update the database with data? There is a connection table between Resource and MetaData and is that table i want to update rather then the metadata table itself. But the connectiontable doesn't have a model class for it self but i exists in the database. How do i update that table only?

EDIT 2enter image description here

The 3 arrow point to rows that looks the same, they should all be the same row but be connected to a different resource in the table to the left.

Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78
  • possible duplicate of [Entity Framework: Duplicate Records in Many-to-Many relationship](http://stackoverflow.com/questions/11646299/entity-framework-duplicate-records-in-many-to-many-relationship) – Gert Arnold Aug 20 '14 at 09:54

4 Answers4

0

you need to switch of the default identity mapping
Ef Fluent API mappings

or use the annotation

[DatabaseGenerated(DatabaseGenerationOption.None)]   
int id {get;set;}

EF annotations

phil soady
  • 11,043
  • 5
  • 50
  • 95
0

You can define and use many-to-many relationships in EF, it doesn't matter if you use a model, or Code First.

There is plenty of information on this if you google "EF many to many relationships".

If you use this EF relationship, you can let EF generate the IDs, and it will do the relationship fix-up for you, and manage the relationship table automatically.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
0

To add / remove relationship you can use ChangeRelationshipState.

For example if you want to add relationship between Metadata 10, 12, 19 And Resource 3.

You can do like this.

using (var db = new AppContext())
{
    var r3 = new Resource { ResourceId = 3 };
    var m10 = new Metadata { MetadataId = 10 };
    var m12 = new Metadata { MetadataId = 12 };
    var m19 = new Metadata { MetadataId = 19 };

    db.Entry(r3).State = EntityState.Unchanged;
    db.Entry(m10).State = EntityState.Unchanged;
    db.Entry(m12).State = EntityState.Unchanged;
    db.Entry(m19).State = EntityState.Unchanged;

    var manager = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager;
    manager.ChangeRelationshipState(r3, m10, h => h.Metadatas, EntityState.Added);
    manager.ChangeRelationshipState(r3, m12, h => h.Metadatas, EntityState.Added);
    manager.ChangeRelationshipState(r3, m19, h => h.Metadatas, EntityState.Added);
    db.SaveChanges();
}
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
-1

add Key Attribute

[Key]
public long MetaDataId { get; set; }
mordechai
  • 829
  • 1
  • 7
  • 23