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,包含了常用框 ...
随机推荐
- CentOS防火墙操作实例(启动、停止、开、闭端口)
注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service iptables status<回车> 停止防火墙: [root@localh ...
- minhash
minhash是一种基于jaccard index 相似度的算法.属于LSH(Location Sensitive Hash)家族中的一员. jaccard index :有两个集合A={a , b ...
- USACO Section 3.1: Contact
算法简单,写起来遇到些小问题 /* ID: yingzho1 LANG: C++ TASK: contact */ #include <iostream> #include <fst ...
- ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute value 1的解决方法
2014-04-16 17:35:30 ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute valu ...
- Ubuntu Server搭建svn服务以及迁移方法【转】
转自:http://www.linuxidc.com/Linux/2013-05/84693.htm Ubuntu Server搭建svn服务以及迁移方法 采用apache+svn,http访问方式. ...
- MapReduce读取hdfs上文件,建立词频的倒排索引到Hbase
Hdfs上的数据文件为T0,T1,T2(无后缀): T0: What has come into being in him was life, and the life was the light o ...
- Idea 使用maven+tomcat的时候,编译指定的Profile
To build a artifact with a profile you have to create a Maven Run/Debug configuration as in the foll ...
- 第四讲:hibernate 的session (二)
Session在hibernate中表示连接.下面介绍他的常用方法. 在使用前先把最重要的说了.打开API,发现在session中基本所有的方法(包括获得session的方法)都会抛出异常Hibern ...
- 基于Linux的oracle数据库管理 part3( 存储 网络 常用命令 )
主要内容 1. 常用存储模式 2. 配置网络 3. 维护指令 常用存储模式 - 文件系统 优点:管理方便, 缺点:读写慢 - 裸设备 只没有被格式化和挂载的磁盘, 只能有程序直接访问, 不能被操作系统 ...
- 转:ViewPager+Fragment基本使用方法(附源码)
ViewPager+Fragment可以做出多页面滑动效果,让我们的应用程序界面操作起来更加灵活 对于ViewPager和Fragment组件还不熟悉的朋友,可以先看看相关的资料 首先在activit ...