Problem when using EF6 c # class error

0

Hi, I'm new to c # I'm trying to use EF6, but when I insert it, it gives me this error that I attached in the image

namespace sng01
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

[Table("sngpos.Cliente")]
public partial class Cliente
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int cedula { get; set; }

    [Required]
    [StringLength(50)]
    public string nombre { get; set; }

    public int tipo { get; set; }

    public int estado { get; set; }
}
}

namespace sng01
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

public partial class sng_db : DbContext
{
    public sng_db()
        : base("name=sng_dbcon")
    {

    }

    public virtual DbSet<Cliente> Cliente { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Cliente>()
            .Property(e => e.nombre)
            .IsUnicode(false);
    }
}

}


 private void button1_Click(object sender, EventArgs e)
    {
       using(var demo = new sng_db())
        {
            Cliente cli = new Cliente() { nombre = "DiegoSNG" };
            demo.Cliente.Add(cli);
            demo.SaveChanges();
        }
    }
    
asked by Diego Mora 01.05.2017 в 21:15
source

1 answer

0

The example below will be in a console project, we must have a database and a table with data

We go by parties, we need the following:

  • MySQL Connector / Net 6.8.x
  • MySQL Server 5.1 or above
  • Entity Framework 6 assemblies
  • .NET Framework 4.0 or above
  • We install Entity Framework (EF) from the NuGet console

    Install-Package EntityFramework
    

    Now we install the compatibility of EF with MySQL

    Install-Package MySql.Data.Entity.EF6
    

    In our app.config we define our connection string

    <connectionStrings>
        <add name="MonkeyFist" connectionString="server=localhost;user id=user;password=mypass;database=mydb" providerName="MySql.Data.MySqlClient" />
    </connectionStrings>
    

    The App config should be something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="MonkeyFist" connectionString="server=localhost;user id=root;password=mypass;database=mydb" providerName="MySql.Data.MySqlClient" />
      </connectionStrings>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    <system.data>
        <DbProviderFactories>
          <remove invariant="MySql.Data.MySqlClient" />
          <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
        </DbProviderFactories>
      </system.data>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    

    The entity we are going to use

    [Table("customer")]
    public class Customer
    {
      [Key]
      [Column("id_customer")]
      public int id { get; set; }
    
      public string customer { get; set; }
    
      public string nit { get; set; }
    
      public string address { get; set; }
    }
    

    The context

    public partial class db_Entities : DbContext
    {
      public db_Entities() : base(nameOrConnectionString: "MonkeyFist") { }
    
      public DbSet<Customer> Customer { get; set; }
    }
    

    The Main

    class Program
    {
      static void Main(string[] args)
      {
        using (var context = new db_Entities())
        {
          var customers = context.Customer.ToList();
          foreach (var cust in customers)
          {
            Console.WriteLine(cust.id + " " + cust.customer + " " + cust.address);
          }
    
        }
      }
    }
    

    The result:

    Reference: link

        
    answered by 01.05.2017 в 21:30