0

I have generated my models from my database. One of the models looks like:

namespace LV.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Ac
    {
        public int IdAc { get; set; }
        public int IdEmp { get; set; }
        public Nullable<System.DateTime> dtd{ get; set; }
        public Nullable<int> Sn{ get; set; }
        public Nullable<bool> A{ get; set; }
        public Nullable<int> IdSi{ get; set; }
        public Nullable<bool> Gest{ get; set; }
        public Nullable<int> IdResp { get; set; }
        public string SigXML { get; set; }
        public Nullable<bool> Read { get; set; }
        public Nullable<System.DateTime> EndDate{ get; set; }
        public string dpd{ get; set; }
        public string gda { get; set; }
        public string typeaq { get; set; }
        public byte[] attch { get; set; }
        public string exten { get; set; }

        public virtual AC emps { get; set; }
    }
}

When I try to create a Controller, it says that has no key defined. I tried the solution from other posts where says that I have to use [Key] and it creates my Controller but when I run the project it throws me another exception that says the following table cannot be created. It says that because the table already exist.

I use Visual Studio 2013 Express with EF 6 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

The primary key is idAc. It is also Identity. If I check the diagram, that's what it says.

From what I've seen, the mapping is good. All my keys, relations, primary keys, identity fields where identified in the diagram.

tereško
  • 58,060
  • 25
  • 98
  • 150
tzortzik
  • 4,993
  • 9
  • 57
  • 88

2 Answers2

0

Try this:

[Key]
[Column("idAc")]
public int IdAc { get; set; }

I suspect that the mapping may be case sensitive and thus, when you specify [Key], EF simply looks for a column named IdAc, not finding it.

I understand that you are using the EDM designer so if that doesn't fix it, it may the case that the EDM designer is messing with the mapping. Try renaming the property to the same as the column (i.e. idAc) or check this workaround.

Also, note that if you don't use [Key] or the Fluent API .HasKey method to indicate which property is the key, by default, EF uses the code-convention of using a property with the same name as the entity (with or without "Id" in the end).

Community
  • 1
  • 1
jnovo
  • 5,659
  • 2
  • 38
  • 56
  • All the columns are named identically to the ones from the database. I don't know if EDM designer is messing the mappings but I know that it reads the table structure and it's relationships very well. – tzortzik Jan 22 '14 at 15:20
0

I found the solution. When I generated the edmx file he asked me if I want to save the connection string. At that point I chose not to save it. Later I've seen that in my edmx properties there was an empty connection string.

I deleted edmx and I generated a new one, this time saving the connection string. Since this moment, everything works smoothly.

tzortzik
  • 4,993
  • 9
  • 57
  • 88