Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using Microsoft.Data.Entity;
- namespace SoTestConsole
- {
- public class Foo
- {
- public long Id { get; set; }
- public string Descr { get; set; }
- }
- public class FooContext : DbContext
- {
- public DbSet<Foo> Foos { get; set; }
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- {
- optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFStackOverFlowTest;Trusted_Connection=True;");
- }
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- modelBuilder.Entity<Foo>().HasKey(x => x.Id);
- modelBuilder.Entity<Foo>().Property(x => x.Id).ValueGeneratedNever();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- using (var db = new FooContext())
- {
- var myFoo = new Foo {Id = 1002, Descr = "Test"};
- db.Add(myFoo);
- db.SaveChanges();
- }
- using (var db = new FooContext())
- {
- var myFoo = db.Foos.FirstOrDefault(x => x.Id == 1002);
- Console.WriteLine($"id = {myFoo?.Id}, descrip = {myFoo?.Descr}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement