SHARE
TWEET

Untitled

a guest Jan 11th, 2016 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Linq;
  3. using Microsoft.Data.Entity;
  4.  
  5. namespace SoTestConsole
  6. {
  7.     public class Foo
  8.     {
  9.         public long Id { get; set; }
  10.         public string Descr { get; set; }
  11.     }
  12.  
  13.     public class FooContext : DbContext
  14.     {
  15.         public DbSet<Foo> Foos { get; set; }
  16.  
  17.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  18.         {
  19.             optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFStackOverFlowTest;Trusted_Connection=True;");
  20.         }
  21.  
  22.         protected override void OnModelCreating(ModelBuilder modelBuilder)
  23.         {
  24.             modelBuilder.Entity<Foo>().HasKey(x => x.Id);
  25.             modelBuilder.Entity<Foo>().Property(x => x.Id).ValueGeneratedNever();
  26.         }
  27.     }
  28.  
  29.     class Program
  30.     {
  31.         static void Main(string[] args)
  32.         {
  33.             using (var db = new FooContext())
  34.             {
  35.                 var myFoo = new Foo {Id = 1002, Descr = "Test"};
  36.                 db.Add(myFoo);
  37.                 db.SaveChanges();
  38.             }
  39.             using (var db = new FooContext())
  40.             {
  41.                 var myFoo = db.Foos.FirstOrDefault(x => x.Id == 1002);
  42.                 Console.WriteLine($"id = {myFoo?.Id}, descrip = {myFoo?.Descr}");
  43.             }
  44.         }
  45.     }
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top