EFSQLserver
1.增加一条烽据
FLYNEWQKEntities dataContext = new FLYNEWQKEntities();
Log log = new Log();
log.Data1 = "tes";
dataContext.Log.Add(log);
dataContext.SaveChanges();
2.修改
FLYNEWQKEntities dataContext = new FLYNEWQKEntities();
Log log = dataContext.Log.Where(t => t.AutoID == 1).Take(1).Single();
log.Data2 = "33";
dataContext.SaveChanges();
3.删除
FLYNEWQKEntities dataContext = new FLYNEWQKEntities();
Log log = dataContext.Log.Where(t => t.AutoID == 1).Take(1).Single();
dataContext.Log.Remove(log);
dataContext.SaveChanges();
4. 列表
FLYNEWQKEntities dataContext = new FLYNEWQKEntities();
var v2 = from t in dataContext.T2_Elderly
join t0 in dataContext.T1_Set_CareLevel on t.CareLevelID equals t0.CareLevelID.ToString() //new { CareLevelID = t.CareLevelID } equals new { CareLevelID = t0.CareLevelID.ToString() }
select t0;
this.gridControl1.DataSource = v2.ToList();
5.事务
//事务
FLYNEWQKEntities dataContext = new FLYNEWQKEntities();
try
{
using (TransactionScope scope = new TransactionScope())
{
//Do something with context1
//Do something with context2
Log log = new Log();
log.Data1 = "tes";
dataContext.Log.Add(log);
//Save and discard changes
dataContext.SaveChanges();
Log log1 = dataContext.Log.Where(t => t.AutoID == log.AutoID).Take(1).Single();
log.Data2 = "33";
//Save and discard changes
dataContext.SaveChanges();
//throw new Exception("dddd");
//if we get here things are looking good.
//scope.Complete();
}
}
catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
6.事务
FLYNEWQKEntities dataContext = new FLYNEWQKEntities();
//if (dataContext.Database.Connection != null)
//{
// dataContext.Database.Connection.Open();
//}
System.Data.Entity.DbContextTransaction tran = dataContext.Database.BeginTransaction();
try
{
Log log = new Log();
log.Data1 = "tes";
dataContext.Log.Add(log);
//Save and discard changes
dataContext.SaveChanges();
Log log1 = dataContext.Log.Where(t => t.AutoID == log.AutoID).Take(1).Single();
log.Data2 = "33";
//Save and discard changes
dataContext.SaveChanges();
//throw new Exception("dddd");
tran.Commit();
MessageBox.Show("保存成功!");
base.Close();
}
catch (Exception ex)
{
tran.Rollback();
MessageBox.Show(ex.Message);
}
7. 函数
https://msdn.microsoft.com/zh-cn/library/dd456847.aspx
http://www.dotblogs.com.tw/programlin/archive/2010/12/16/20179.aspx
1.Model-Defined Functions的定義是在Conceptual Model,而UDF需定義在Storage Model.
而設定的就如同EF設計stored procedure一樣,只是在一些參數上有所不同,但不幸的VS內建的EDM tools只能設計stored procedure
而無法定義UDF,所以請依上篇文章做法手動用XML Editor開啟EDM File.開啟後在<edmx:StorageModels>區段後加上
<Function Name="MYGETYEAR" ReturnType="int" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="time" Type="datetime" Mode="In" />
</Function>
這邊的XML跟上篇文章很像,差異點在於UDF是定義於Storage Model故所有的資料型別都是sql type,這邊就不再多加說明了,紅字以外的描述都直接套用即可.
2.修改完 EDM後一樣的如同Model-Defined Functions上篇文章中的步驟二也必須在程式端定義一個類別來供給LINQ套用唯一的差別在於EdmFunctionAttribute的第一個參數namespace
改為EDM Storage Model區段中的namespace,可以在<edmx:StorageModels>後的<Schema Namespace="TestModel.Store" ../>取得
故在程式最後修改如下
要示写到 静态类,中用静态函数的来写
public static class DbFunctions
{
//[EdmFunction("FLYNEWQKModel.Store", "fun_GetPY" )]
[DbFunction("FLYNEWQKModel.Store", "fun_GetPY")]
public static string fun_GetPY(string Str)
{
throw new NotSupportedException("Direct calls are not supported.");
}
[DbFunction("FLYNEWQKModel.Store", "fun_GetFeeCount")]
public static decimal? fun_GetFeeCount(string ElderlyID, DateTime Time1)
{
throw new NotSupportedException("Direct calls are not supported.");
}
[DbFunction("FLYNEWQKModel.Store", "fun_GetHYAmount")]
public static decimal? fun_GetHYAmount(string FZHNumber)
{
throw new NotSupportedException("Direct calls are not supported.");
}
}
在<edmx:StorageModels> 段增加对函数的定义
<Function Name="fun_GetFeeCount" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" ReturnType="money">
<Parameter Name="ElderlyID" Type="nvarchar" Mode="In" />
<Parameter Name="Time1" Type="datetime" Mode="In" />
</Function>
<Function Name="fun_GetHYAmount" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" ReturnType="money">
<Parameter Name="FZHNumber" Type="nvarchar" Mode="In" />
</Function>
<Function Name="fun_GetPY" ReturnType="varchar" Aggregate="false" BuiltIn="false" Schema="dbo" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" >
<Parameter Name="Str" Type="varchar" Mode="In" />
</Function>
var v1 = from c in dataContext.T2_Elderly select new { c.ElderlyName, a = SqlFunctions.GetDate(), b = DbFunctions.fun_GetPY(c.ElderlyName), c = DbFunctions.fun_GetFeeCount(c.ElderlyID, SqlFunctions.GetDate().Value), d = DbFunctions.fun_GetHYAmount(c.ElderlyID) };
EFSQLserver的更多相关文章
- 使用.net core abp framework
abp是一个有用的框架,包含许多功能,可以用来作为脚手架. 直接在官方网站上输入相应的工程名称,选择对应的版本就会下载对应的版本..net core 版本的可以使用后端框架部分来做api,包含了常用框 ...
随机推荐
- Java:异常的处理
异常分两种大的异常类型,运行时异常和受检查异常. 用户既可以使用系统的异常类来处理异常信息,也可以创建系统的异常类的子类来自定义异常,这种方式比较灵活,虚拟机可以报出自己设置的异常信息,清楚明白. 1 ...
- ADODB.Connection 错误 '800a0e7a'。。
今天帮同学调程序的时候发现的:错误提示如下: ADODB.Connection 错误 '800a0e7a' 未找到提供程序.该程序可能未正确安装. /hua1/manage/inc/conn.asp, ...
- xcode解决问题dyld: Library not loaded
一.问题 编译通过,联机调试时,应用启动闪退,XCODE的Output出现提示: dyld: Library not loaded: /System/Library/Frameworks/AdSupp ...
- linux目录
转自:http://www.educity.cn/it/linux/201002040848221715.htm 对于linux新手来说,最感到迷惑的问题之一就是文件都存在哪里呢?特别是对于那些从wi ...
- 总结Allegro元件封装(焊盘)制作方法[修整]
总结Allegro元件封装(焊盘)制作方法 在Allegro系统中,建立一个零件(Symbol)之前,必须先建立零件的管脚(Pin).元件封装大体上分两种,表贴和直插.针对不同的封装,需要制作不同的P ...
- html5 移动端单页面布局
序 移动端的web网页使用的是响应式设计,但一般我们看到的网站页面都是跳转刷新得到的,比如通过点击一个menu后进入到另一个页面 今天来说下是移动端的单页面的布局.单页面就是一切操作和布局都是 ...
- 【转】对Android开发者有益的40条优化建议
下面是开始Android编程的好方法: 找一些与你想做事情类似的代码 调整它,尝试让它做你像做的事情 经历问题 使用StackOverflow解决问题 对每个你像添加的特征重复上述过程.这种方法能够激 ...
- 《OD学hadoop》第一周0626 作业二:Linux基础
一.打包压缩 知识点: tar -zxvf -C PATH tar -jxvf tar -zcvf tar -jcvf tar:打包命令 -z 打包同时gzip压缩 -j 打包同时bzip2 -c 打 ...
- bzoj3668: [Noi2014]起床困难综合症
从高位到低位枚举期望的应该是ans最高位尽量取一.如果该数最高位为o的话能够取得1直接更新ans否则判断该位取1是否会爆m不会的话就加上. #include<cstdio> #includ ...
- 用sql获取某字符串中的数字部分的语句
create function dbo.F_Get_No ( @No varchar(100) ) RETURNS bigint AS BEGIN WHILE PATINDEX('%[^0-9]%', ...