Last active
August 29, 2015 13:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;
}
namespace DemoCodeFirsts
{
using System.Data.Entity;
}
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]