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,包含了常用框 ...
随机推荐
- 配置Linux系统网卡连接网络
本实验需要两台虚拟机来完成,环境如下: 主机名称 操作系统 ip地址 本地主机 RHEL7_64 192.168.157.134 -> 192.168.129.127 远程主机 CentOS 6 ...
- Linux系统文件的隐藏属性
linux系统的文件除了有普通rwx权限外还有一种隐藏权限,例如明明有权限删除某个文件却报错了. 或者仅能为某个文件追加内容而不能减少内容,遇到这种很‘奇怪’的文件,就要怀疑是文件被设置隐藏权限了. ...
- android 四种堆状态
总结下: ====> 建议首先阅读下面两篇文章,这样才可以更好的理解Activity的加载模式: Android的进程,线程模型 http://www.cnblogs.com/ghj1976/a ...
- java遍历Map时remove删除元素
public class T { /** * @param args */ public static void main(String[] args) { // TODO Auto-generate ...
- Vi Usage
标签: linux 编辑工具 md 快捷键以及常用命令(前面带:的是命令) h -> 左移一个字符 j -> 下移一行 k -> 上移一行 l -> 右移一个字符 w或Shif ...
- 批处理 —— 每天生成一个以日期命名的文件(Win XP)
想达到这样一个效果:每天在某个目录下生成一个以日期命名的文件(如,0705.txt). 第一步,新建一个批处理文件 新建一个文件,比如[create_day_file.bat].编辑,输入以下内容 : ...
- python 中的map 详解
python中的map函数应用于每一个可迭代的项,返回的是一个结果list.如果有其他的可迭代参数传进来,map函数则会把每一个参数都以相应的处理函数进行迭代处理.map()函数接收两个参数,一个是函 ...
- 串口log
使用windows自带的超级终端: 可从开机log开始保存: -----
- 了解thinkphp(二)
ThinkPHP的核心文件: Library文件夹下的Think文件夹目录 ThinkPHP的入口文件是: ThinkPHP.php , 我们在创建项目时都要引入这个入口文件!!! 一 , 项目的部署 ...
- C#操作office进行Excel图表创建,保存本地,word获取
,新建C#控制台应用程序(Excel创建图表) using System; using System.Collections.Generic; using System.Linq; using Sys ...