entity framework insert code example
Example 1: entity framework insert
var id = Guid.NewGuid();
using (var db = new EfContext("name=EfSample"))
{
var customers = db.Set<Customer>();
customers.Add( new Customer { CustomerId = id, Name = "John Doe" } );
db.SaveChanges();
}
Example 2: entity save example in c# model first
using (var context = new BloggingContext())
{
context.Blogs.Add(new Blog { Url = "http://example.com/blog" });
context.Blogs.Add(new Blog { Url = "http://example.com/another_blog" });
context.SaveChanges();
}
using (var context = new BloggingContext())
{
context.Blogs.Add(new Blog { Url = "http://example.com/blog_one" });
context.Blogs.Add(new Blog { Url = "http://example.com/blog_two" });
var firstBlog = context.Blogs.First();
firstBlog.Url = "";
var lastBlog = context.Blogs.Last();
context.Blogs.Remove(lastBlog);
context.SaveChanges();
}