entity save example in c# model first
using (var context = new BloggingContext())
{
// seeding database
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())
{
// add
context.Blogs.Add(new Blog { Url = "http://example.com/blog_one" });
context.Blogs.Add(new Blog { Url = "http://example.com/blog_two" });
// update
var firstBlog = context.Blogs.First();
firstBlog.Url = "";
// remove
var lastBlog = context.Blogs.Last();
context.Blogs.Remove(lastBlog);
context.SaveChanges();
}
3.67
6
exec sp_executesql N'INSERT [dbo].[Student]([StudentName], [StandardId])
VALUES (@0, NULL)
SELECT [StudentID] FROM [dbo].[Student]
WHERE @@ROWCOUNT > 0 AND [StudentID] = scope_identity(),@0='Bill'
Thank you!
6
0
3.67
6
using (var context = new BloggingContext())
{
var blog = context.Blogs.First();
blog.Url = "http://example.com/blog";
context.SaveChanges();
}
Thank you!
6
0
4.25
4
using (var context = new BloggingContext())
{
var blog = context.Blogs.First();
context.Blogs.Remove(blog);
context.SaveChanges();
}
Thank you!
4
0
3
2
// disconnected existing entity
var student = new Student(){ StudentId = 1, StudentName = "Steve" };
using (var context = new SchoolDBEntities())
{
context.Entry(student).State = student.StudentId == 0? EntityState.Added : EntityState.Modified;
context.SaveChanges();
}
Thank you!
2
0
4.17
6
// disconnected new entity
var student = new Student(){ StudentName = "Bill" };
using (var context = new SchoolDBEntities())
{
context.Entry(student).State = student.StudentId == 0? EntityState.Added : EntityState.Modified;
context.SaveChanges();
}
Thank you!
6
0
Are there any code examples left?
New code examples in category C#
-
C# 2022-03-27 22:40:23 c# movement script
-
C# 2022-03-27 22:15:17 Delete last modification on EntityFramework Core
-
C# 2022-03-27 22:15:02 file.create folder c#
-
C# 2022-03-27 19:25:04 Error inflating class android.support.constraint.ConstraintLayout
-
C# 2022-03-27 19:15:02 top down shooting in unity 2D
-
C# 2022-03-27 18:50:02 c# remove invalid directory characters
-
C# 2022-03-27 18:30:07 how to use K2 games Games parallax background
-
C# 2022-03-27 18:25:10 c# replace crlf
-
C# 2022-03-27 18:25:09 keycode for left mouse click unity