Skip to content

Instantly share code, notes, and snippets.

@franciscojunior
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franciscojunior/8909030 to your computer and use it in GitHub Desktop.
Save franciscojunior/8909030 to your computer and use it in GitHub Desktop.
EF sample for #160 - Returning DB generated values on insert for EF fixing #159
<?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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql" />
<add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="BloggingContext" providerName="Npgsql" connectionString="server=10.0.2.2;port=5484;userid=npgsql_tests;password=npgsql_tests;database=ef_code_first_returning_generated_value" />
</connectionStrings>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
namespace a
{
class Program
{
static void Main(string[] args)
{
using (var db = new BloggingContext())
{
var blog = new Blog { Name = "George" };
db.Blogs.Add(blog);
db.SaveChanges();
Console.WriteLine(blog.GuidGeneratedByPostgreDatabase);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
/*
* create schema "dbo";
* fast hack for uuid missing support:
create function returnuuidfortesting() returns uuid as $$ select 'c2d29867-3d0b-d497-9191-18a9d8ee7830'::uuid $$ language sql;;
create table "dbo"."Blogs"( "BlogId" serial, "Name" text, "GuidGeneratedByPostgreDatabase" uuid default returnuuidfortesting());
create table "dbo"."Posts" ("PostId" serial, "Title" text, "Content" text);
*
*/
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public Guid GuidGeneratedByPostgreDatabase { get; set; }
public virtual List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
}
@micro4dev
Copy link

Hi I get an error when trying to run a project code example is as follows:

namespace DemoCodeFirsts
{
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("sucursal", Schema = "public")]
public class Sucursal
{
    [Key]
    public virtual int Idsucursal{ get; set; }
    public virtual string Codigosucursal{ get; set; }
    public virtual string Nombresucursal { get; set; }
    public virtual int Iddistrito { get; set; }
    public virtual string Direccionsucursal { get; set; }
}

}

namespace DemoCodeFirsts
{
using System.Data.Entity;

public class ErpTransporteContext : DbContext
{
    public ErpTransporteContext(): base(nameOrConnectionString: "ErpTransporte")
    {
    }
    public DbSet<Sucursal> Sucurales { get; set; }
}

}

Exception:Thrown: "ERROR: 42P01: relation "dbo.EdmMetadata" does not exist" (Npgsql.NpgsqlException)
A Npgsql.NpgsqlException was thrown: "ERROR: 42P01: relation "dbo.EdmMetadata" does not exist"
Time: 04/04/2014 11:49:57 p.m.
Thread:Main Thread[3884]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment