3

I have two classes Class1 and Class2

[Table("CLASS1", Schema = "public")]
    public class Class1
    {
        public int Class1Id { get; set; }
        public string Name1 { get; set; }
    }

[Table("CLASS2", Schema = "public")]
    public class Class2
    {
        public int Class2Id { get; set; }
        public string Name2 { get; set; }

        public int Class1Id { get; set; }
        public virtual Class1 c1 { get; set; }

I defined the DatabaseContext :

public sealed class     DatabaseContext : DbContext
    {
        private static readonly DatabaseContext instance = new DatabaseContext();


        public static DatabaseContext Instance
        {
            get
            {
                return instance;
            }
        }

        private DatabaseContext()
            : base("maconnexion")
        {
            Database.SetInitializer<DatabaseContext>(null);

        }


        public DbSet<Class1> table1{ get; set; }
        public DbSet<Class2> table2{ get; set; }

    }

and i have added those lines to my App.config

<DbProviderFactories>
      <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>

 <connectionStrings>
    <add name="maconnexion"
 connectionString="Server=127.0.0.1;Port=5432;Database=DB_Ticketing;UserId=postgres;Password=slots2013;" providerName="Npgsql" />
  </connectionStrings>

and I have created a database in PostgreSQL with the name DB_Ticketing. But when I execute my code the tables table1 and table2 are not added to my data base.

Devart
  • 119,203
  • 23
  • 166
  • 186
Alkao12
  • 110
  • 1
  • 2
  • 8

3 Answers3

1

Configure your application as follows: Step 1 (configure App.config) (copy and paste)

<?xml version="1.0"?>
 <configuration>
  <configSections>
   <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="maconnexion" connectionString="User Id=postgres;Password=slots2013;Host=localhost;Database=DB_Ticketing;" providerName="Npgsql"/></connectionStrings>
  <startup>
  <supportedRuntime version="v4.0"sku=".NETFramework,Version=v4.0,Profile=Client"/>
  </startup>
   <entityFramework>
  <providers>
   <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices,Npgsql.EntityFramework">
   </provider>
  </providers>
 <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory,Npgsql"/>
   </entityFramework>
   <system.data>
   <DbProviderFactories>
  <remove invariant="Npgsql"></remove>
  <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql"/>
   </DbProviderFactories>
</system.data>

Step 2 (Configure your ContextDB)

 public class NameContext:DbContext   
 {
 public NameContext():base(nameOrConnectionString:"maconnexion"){}
    public DbSet<Class1> Class1s{ get; set; }
    public DbSet<Class2> Class2s{ get; set; }}

Step 3 ( calling a method Insert)

public static void Insert(Class1 o)
        {
         using (var context = new NameContext())
            { //Where "o" it is an object, for example Class1 o=new Class1();
                context.Class1s.Add(o);
                context.SaveChanges();
            }}

Nota: I recommend use of the DataAnnotations for create your business class, for example: [Table("MyTable", Schema = "MySchema")] public class ob_group { [Key] [Column("nseq_grp")] public int Nseq_grp {get;set;} for more details, you can see: http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx I hope it's useful

Marinpietri
  • 112
  • 3
  • 8
1

I know has been a long time, but for the people still gets in this post. this is the most simple info to do it. basic, how to configure. Entity Framework 6 with PostgreSQL: http://lvasquez.github.io/2014-11-24/EntityFramework-PostgreSql/

how to init code-first. very graphic. Entity Framework Code First For PostgreSQL: https://dotblogs.com.tw/wuanunet/2016/02/26/entity-framework-code-first-postgresql

Optional. Avoid pluralize: How turn off pluralize table creation for Entity Framework 5?

Note: if you decide not to use code-first, remember PostgreSQL by default sets lowercase, so define if decorate with lower case or create tables case sensitive (create table public."XyZa")

CesarD
  • 573
  • 14
  • 30
ERai
  • 133
  • 8
0

i did it using sql query

SELECT 'public class '||c.table_name ||' { '|| 
 string_agg(case when c.character_maximum_length is not null then '[StringLength(' || to_char(c.character_maximum_length,'9999')||')]'  else '' end   ||
 'public ' ||
(case when c.datetime_precision is not null then ' DateTime' else 
case when c.numeric_precision is not null then ' int' else 'string' end  end)||
case c.is_nullable when 'YES' then '?'end ||  ' ' ||c.column_name,'{get;set;} ') ||' {get;set;} }'
  FROM information_schema.columns c
  where c.table_schema ='public'
 group by c.table_name
CodeArt
  • 858
  • 8
  • 10