-1

I have created with EF designer this simple diagram and the code has been generated, but when I generate the solution I get this error :

Erreur 1 Erreur 3004 : Problème de fragments de mappage à partir de la ligne 84 :Aucun mappage n'est spécifié pour les propriétés intDB.tpintDB_id_tpint dans Jeu intDBs. Une entité avec clé (PK) n'effectuera pas d'aller-retour lorsque : [Entité] is type [helpdeskModel.intDB]

this is a link to an image of my diagram

Here is the code :

    public partial class helpdeskEntities : DbContext
    {
        public helpdeskEntities()
            : base("name=helpdeskEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<intDB> intDBs { get; set; }
        public DbSet<tpintDB> tpintDBs { get; set; }
    }
}




     public partial class intDB
{
    public int ID { get; set; }
    public Nullable<System.DateTime> debint { get; set; }
    public Nullable<System.DateTime> finint { get; set; }
    public Nullable<int> id_int { get; set; }
    public decimal id_tpint { get; set; }
    [ForeignKey("id_tpint")]
    public virtual tpintDB tp_intDB { get; set; }
}
    }





      public partial class tpintDB
    {
        public decimal id_tpint { get; set; }
        public string libelle { get; set; }
        public string desc_tpint { get; set; }

        public virtual ICollection<intDB> intDBs { get; set; }
    }
    }

My new intDB Model :

public partial class intDB
{
    public int ID { get; set; }
    public Nullable<System.DateTime> debint { get; set; }
    public Nullable<System.DateTime> finint { get; set; }
    public Nullable<int> id_int { get; set; }
    [ForeignKey("id_tpint")]
    public virtual int tp_intDB { get; set; }
}
biba
  • 3
  • 3

2 Answers2

1

We can't tell for sure what the problem is without a piece of code (i.e. the entity classes). But the problem seems obvious to me: in the intdb entity you need this property: public virtual tpintDB tpintDB { get; set; } and in the tpintDB you need public ICollection <intDB> intDBList { get; set; }. Also, make sure you enable automatic migrations or add the migration yourself.

UPDATE

I updated my answer so you can better see how the entity should be declared.

public partial class intDB
{
    public int ID { get; set; }
    public Nullable<System.DateTime> debint { get; set; }
    public Nullable<System.DateTime> finint { get; set; }
    public Nullable<int> id_int { get; set; }
    public int id_tpint { get; set; }
    [ForeignKey("id_tpint")]
    public virtual tpintDB tp_intDB { get; set; }
}

UPDATE 2

Also, the tpintDB needs to look like this.

public partial class tpintDB
{
    public int id_tpint { get; set; }
    public string libelle { get; set; }
    public string desc_tpint { get; set; }

    public virtual ICollection<intDB> intDBs { get; set; }
}

Think at the ICollection as an instrument for the tpintDB to remember which intDB objects refer to it. My english is not the best right now, but I hope you understood :D Also, i don't guarantee that this is the best solution, but it worked for my 30+ entities in a project and I think it's pretty clean.

Andrei Mișcu
  • 139
  • 10
  • How can I add the migration ? – biba Aug 28 '15 at 11:35
  • Open the package manager console and write `add-migration [randomName]`. It's that simple – Andrei Mișcu Aug 28 '15 at 11:38
  • Now i have this :Error 1 error 2039: The conceptual property "id_tpint" has already been mapped to a storage property with the type "int". If the conceptual property is mapped to several properties in the storage model, make sure that all the properties of the storage model have the same type. – biba Aug 28 '15 at 11:48
  • Presuming you get this error for the intDB entity, you need to declare the `id_tpint` property as `int`, since it will contain the id of the referred entity. Also, if this doesn't solve this problem, use DataAnnotations to tell EF what property you want to have as a FK. Like: `[ForeignKey ("id_tpint")]` above the virtual tpintDB property. More info [here](http://www.codeproject.com/Articles/319366/EF-Code-First-Add-a-Foreign-Key-relationship) – Andrei Mișcu Aug 28 '15 at 12:02
  • It doesn't work with me .. I editted my code you can verify if it is coorect or not – biba Aug 28 '15 at 14:09
  • 1. You can use int? instead of Nullable. 2. The virtual property must be an instance of your referred class so it must be of type tpintDB (see my answer). 3. You must keep the id_tpint property, not delete it. In the annotation you refer to it as EF will map it into the database. So, your intDB foreign key should be like this `public int id_tpint { get; set; } [ForeignKey("id_tpint")] public virtual tpintDB tp_intDB { get; set; } }` More on Nullable props [here](http://stackoverflow.com/questions/10710393/nullable-property-to-entity-field-entity-framework-through-code-first) – Andrei Mișcu Aug 28 '15 at 15:55
  • Now I have this : La propriété conceptuelle « id_tpint » a déjà été mappée à une propriété de stockage avec le type « int ». Si la propriété conceptuelle est mappée à plusieurs propriétés dans le modèle de stockage, assurez-vous que toutes les propriétés du modèle de stockage ont le même type – biba Aug 30 '15 at 20:36
  • Try replacing the type of `id_tpint` to `decimal` instead of `int` in both entities. Please update the question with the current code so we can see the problem. – Andrei Mișcu Aug 31 '15 at 11:03
  • I updated the code and then here is a simple photo .. I still have the same problem: http://www.mediafire.com/view/3csgz1g13rywipc/cap.JPG – biba Sep 01 '15 at 14:08
  • I just created a new MVC application and copied your code there and it works like a charm. Try debugging it to the smallest piece of data and see what is wrong. – Andrei Mișcu Sep 01 '15 at 16:44
0

Please Check the DataType of Id_tpint are both same. Also you must define Primary and Foreign key Id_tpint for intDB and tpintDB.

ABM Abdul Muttalib
  • 195
  • 1
  • 2
  • 13