Entity Framework select
ref https://msdn.microsoft.com/zh-tw/library/bb399739(v=vs.100).aspx
產生 School .edmx 檔案 (Entity Framework 快速入門)
//select
TravelEntities schoolContext = new TravelEntities();
using (TravelEntities te = new TravelEntities())
{
var user = from a in te.Spots
where a.Id == ListBoxSpot.SelectedValue
select a;
foreach (Spot num in user)
{
TextBoxName.Text = num.Tel;
}
}
// like
var user = from a in te.Hotels
where a.Addr.Contains("tw")
select a;
//modify
TravelEntities schoolContext = new TravelEntities();
using (TravelEntities te = new TravelEntities())
{
var user = from a in te.Spots
where a.Id == ListBoxSpot.SelectedValue
select a;
foreach (Spot num in user)
{
num.Name = num.Name + "_";
schoolContext.Entry(num).State = EntityState.Modified;
schoolContext.SaveChanges();
}
}
from https://blog.csdn.net/weixin_43602710/article/details/93638575
TestDbContext db = new TestDbContext(); var test = db.Tests.Find(1); test.Remarks = "更新字段方法1"; db.SaveChanges();
TestDbContext db = new TestDbContext(); Test test = new Test() { ID = 1, Remarks = "更新字段方法2" }; db.Entry(test).State = System.Data.Entity.EntityState.Modified; db.SaveChanges();
TestDbContext db = new TestDbContext(); Test test = new Test() { ID = 1, Remarks = "更新字段方法3" }; db.Tests.Attach(test); db.Entry(test).Property("Remarks").IsModified = true; db.SaveChanges();
exec sp_executesql N‘UPDATE [dbo].[Tests] SET [Remarks] = @0 WHERE ([ID] = @1) ‘,N‘@0 nvarchar(max) ,@1 bigint‘,@0=N‘更新字段方法1‘,@1=1
exec sp_executesql N‘UPDATE [dbo].[Tests] SET [Name] = NULL, [Email] = NULL, [Remarks] = @0 WHERE ([ID] = @1) ‘,N‘@0 nvarchar(max) ,@1 bigint‘,@0=N‘更新字段方法2‘,@1=1
exec sp_executesql N‘UPDATE [dbo].[Tests] SET [Remarks] = @0 WHERE ([ID] = @1) ‘,N‘@0 nvarchar(max) ,@1 bigint‘,@0=N‘更新字段方法3‘,@1=1
from https://medium.com/sally-thinking/%E7%A8%8B%E5%BC%8F%E5%AD%B8%E7%BF%92%E4%B9%8B%E8%B7%AF-day28-c-ado-net-entity-framework-%E7%89%A9%E4%BB%B6%E9%97%9C%E8%81%AF%E5%B0%8D%E6%87%89-4b27943af679
using (var db = new Model1())
{ // Create and save a new Blog
var account = new Account { ID = "3",PW = "1", DeptID = 1 }; //造一個Blog加到Blogs資料集裡面
db.Account.Add(account); //存入db資料內容類別裡
db.SaveChanges();
// Display all Blogs from the database
var query = from b in db.Account
where b.ID == login.UserName && b.PW==login.Password
orderby b.ID
select b;
//Console.WriteLine(“All blogs in the database:”);
foreach (var item in query)
{
Console.WriteLine(item.ID);
} //將資料列成清單
Console.WriteLine("Press any key to exit…");
Console.ReadKey();
}
}
//remove
using (var db = new Model1())
{
var account = db.Account.Find(ID);
db.Account.Remove(account);
db.SaveChanges();
}
namespace Finance
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Account")]
public partial class Account
{
[StringLength(20)]
public string ID { get; set; }
[Required]
[StringLength(20)]
public string PW { get; set; }
public int DeptID { get; set; }
public int? Enable { get; set; }
public DateTime? AddDate { get; set; }
}
}
from https://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record
//update
db.Users.Attach(updatedUser);
var entry = db.Entry(updatedUser);
entry.Property(e => e.Email).IsModified = true;
// other changed properties
db.SaveChanges();
var original = db.Users.Find(updatedUser.UserId);
if (original != null)
{
original.BusinessEntityId = updatedUser.BusinessEntityId;
original.Email = updatedUser.Email;
db.SaveChanges();
}
加入>新增項目>Data>ADO.NET實體模型
補充:實體資料模型
實體資料模型 (EDM) 是描述資料結構的概念集,不論其預存形式為何。
模型:EF Designer
備註:
- 不把連線字串寫死,而是寫成設定檔
- 將App.Config中的連接設定儲存為 的用意?
重要性:連線字串為資料類別的名稱,程式設計會使用到
注意:必須勾選產生的物件名稱複數化或單數化
用意:單數負數正規化,避免程式設計過程中的混肴
留言
張貼留言