First of all, your solution is outdated. You need an update of all the solution packages. Open the Package Manager Console and type in the following:
PM> Update-Package
There was some weird problem with your packages, so I had to delete the directory packages
solution and create again through the same command. It may be necessary to close and reopen the following pro solution.
The Code First is not enabled in the project. It means that there is no support for Migrations (incremental database migrations), or context modification control. Open the Package Manager Console and type in the following:
PM> Enable-Migrations
Setting up the setting caught this error:
The Property 'Nivel_acessoid' cannot be configured as a navigation Property. The Property must be a Valid Entity type and the Property should have a non-abstract getter and Setter. For Collection properties the type must implement Icollection Where T is a Valid Entity type.
It means that you set up a wrong ID, that is, a real database parameter, and the Entity Framework understood the ID as a navigation property, which is just one more property to load other information from related entities. I needed to change the following:
namespace SistemaC.Models
{
public class Acesso
{
[Key]
public int AcessoId { get; set; }
public int NivelAcessoId { get; set; } // Isto é uma informação de banco.
[Required]
public String Usuario { get; set; }
[DataType(DataType.Password)]
public String Senha { get; set; }
public virtual NivelAcesso NivelAcesso { get; set; } // Isto é um atributo de navegação.
}
}
Notice I took [ForeignKey("Nivel_AcessoID")]
. It serves for navigation properties, not key properties. As its project is very simple, the Entity Framework knows how to identify navigation properties by itself and by naming them. You can therefore not use the attribute.
In the same way, I changed NivelAcesso
:
namespace SistemaC.Models
{
public class NivelAcesso
{
[Key]
public int NivelAcessoId { get; set; }
public String Nivel { get; set; }
public virtual ICollection<Acesso> Acessos { get; set; }
}
}
The convection of names does not use "_" in object names. Another thing I needed to do is identify which is the key to the Model with the attribute [Key]
.
ICollections
related are always with plural names, so you know during programming that you are dealing with a collection, and not with a single object.
The context was like this:
namespace SistemaC.Models
{
public class ClinicaDbContext : DbContext
{
//public ClinicaDBContext()
//{
// Database.SetInitializer<ClinicaDBContext>(new CreateDatabaseIfNotExists<ClinicaDBContext>());
//}
public DbSet<NivelAcesso> NivelAcessos { get; set; }
public DbSet<Acesso> Acessos { get; set; }
}
}
Everything also in the plural.
I had to erase the DBInit.cs
. He was totally wrong. The Global.asax
was like this:
namespace SistemaC
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
Fixed that, I managed to generate the first Migration the one that actually installs the database, so:
PM> Initial Add-Migration
Spawned:
namespace SistemaC.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class Inicial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Acessoes",
c => new
{
AcessoId = c.Int(nullable: false, identity: true),
NivelAcessoId = c.Int(nullable: false),
Usuario = c.String(nullable: false),
Senha = c.String(),
})
.PrimaryKey(t => t.AcessoId)
.ForeignKey("dbo.NivelAcessoes", t => t.NivelAcessoId, cascadeDelete: true)
.Index(t => t.NivelAcessoId);
CreateTable(
"dbo.NivelAcessoes",
c => new
{
NivelAcessoId = c.Int(nullable: false, identity: true),
Nivel = c.String(),
})
.PrimaryKey(t => t.NivelAcessoId);
}
public override void Down()
{
DropForeignKey("dbo.Acessoes", "NivelAcessoId", "dbo.NivelAcessoes");
DropIndex("dbo.Acessoes", new[] { "NivelAcessoId" });
DropTable("dbo.NivelAcessoes");
DropTable("dbo.Acessoes");
}
}
}
"Gypsy, the table name is Accesses, nay Accessions, and Levels, nay Levels". All right. The pluralization of the Entity Framework is in English. That’s why the name. We can arrange as follows:
namespace SistemaC.Models
{
[Table("Acessos")]
public class Acesso
{ ... }
}
namespace SistemaC.Models
{
[Table("NiveisAcessos")]
public class NivelAcesso
{ ... }
}
Upshot:
namespace SistemaC.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class Initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Acessos",
c => new
{
AcessoId = c.Int(nullable: false, identity: true),
NivelAcessoId = c.Int(nullable: false),
Usuario = c.String(nullable: false),
Senha = c.String(),
})
.PrimaryKey(t => t.AcessoId)
.ForeignKey("dbo.NiveisAcessos", t => t.NivelAcessoId, cascadeDelete: true)
.Index(t => t.NivelAcessoId);
CreateTable(
"dbo.NiveisAcessos",
c => new
{
NivelAcessoId = c.Int(nullable: false, identity: true),
Nivel = c.String(),
})
.PrimaryKey(t => t.NivelAcessoId);
}
public override void Down()
{
DropForeignKey("dbo.Acessos", "NivelAcessoId", "dbo.NiveisAcessos");
DropIndex("dbo.Acessos", new[] { "NivelAcessoId" });
DropTable("dbo.NiveisAcessos");
DropTable("dbo.Acessos");
}
}
}
Finally, update the base with the command:
PM> Update-Database
The bank will be created with the tables, according to the origin pointed in its Connection String
. In fact, I needed to change it slightly to allow authentication with Windows and I didn’t need to use the user sa
:
<connectionStrings>
<add name="ClinicaDBContext" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=Clinica;Integrated Security=SSPI;MultipleActiveResultSets=true;" />
</connectionStrings>
Done that, compiled with bank and all. You can download the fonts here.
Face your connection string is automatically generated, it is not generating your EDMX?
– Marconi
Automatically generated? did not know it and even about EDMX I had no knowledge. I read a series of handouts and none of them quoted the EDMX, now that you spoke I did a search and found out what it is, however if the . edmx is listed in the directory structure, it has not been created.
– Cleiton Ribeiro
this tutorial is very good, follow the step by step you will be able to create. http://www.macoratti.net/11/09/ef4_mp1.htm
– Marconi
it is generated in the structured directory. it shows you the database template. as well as its generated objects through its tables.
– Marconi
From what I can see, the. edmx is created from an existing base... but in my case, the intention is to generate a base from my created entities.
– Cleiton Ribeiro
Do I understand you’re doing the reverse way? More because this?
– Marconi
Ué... because the great attraction of the ORM is precisely this, not to worry about coding SQL and missing the need to build database structures inside the server.
– Cleiton Ribeiro
Cleiton, try it this way. If it works I put in response: In your Connection string instead of putting this name "Connection", put the name of your Dbcontext, ie, "Clinicadbcontext". Just to see if it works. And see later, because its string connection is actually automatically generated !
– Érik Thiago
I had already tried that and also it did not work... I tried again and also did not give any result.
– Cleiton Ribeiro
I added the project download link.
– Cleiton Ribeiro
Cleiton I opened your project and saw that much is missing, and also the way it is being done will not create the database according to your classes. I found a good tutorial that can help you, which teaches from the beginning of the project. http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
– TonCunha
What is missing, for example? I already have the model classes, I already have Webconfig configured and with connection string, I already have the Dbcontext class with the proper dbsets, I already have a controller that offers access to my main view... I had already consulted this page, but found no discrepancy.
– Cleiton Ribeiro
Cleiton a tip for you that learning: go to the official website and do the Getting Started... It is better to do it from the beginning. There it will surely help you to find your mistake.
– Érik Thiago
I will download your code here and I already answer. You do not prefer to use a Github or something that is easier for people to suggest modification in your code?
– Leonel Sanches da Silva