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的更多相关文章

  1. 使用.net core abp framework

    abp是一个有用的框架,包含许多功能,可以用来作为脚手架. 直接在官方网站上输入相应的工程名称,选择对应的版本就会下载对应的版本..net core 版本的可以使用后端框架部分来做api,包含了常用框 ...

随机推荐

  1. Intellij编译时报“java: System Java Compiler was not found in classpath” 解决办法

    Intellij编译时报“java: System Java Compiler was not found in classpath” 解决方法: Project Settings > Comp ...

  2. mysql C api

    1.初始化一个链接结构. 2.创建一个链接. 3.执行查询. 4.关闭链接. MYSQL* conn; 首先,声明一个conn指针指向一个MYSQL结构体,这个结构体就是一个数据库连接句柄. conn ...

  3. Java应用发布后,需要关注的7个性能指标

    在某个重大发布之后,都需要记录相应的指标,本文介绍了最重要的几个 Java 性能指标,包括响应时间和平均负载等.为理解应用程序在生产环境中如何运行,就需要遵循一些 Java 性能指标. 在以前,当软件 ...

  4. html中offsetTop、clientTop、scrollTop、offsetTop各属性

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  5. 使用Windows Azure PowerShell远程管理Windows Azure虚拟机

    对于Windows Azure,如果你还在使用windowsazure.com门户来管理虚拟机,那就显得不怎么高上大了.Windows Azure PowerShell 是一个功能强大的脚本环境,可用 ...

  6. Android 动画 setVisibility 后出错解决方法

    ===先说明下背景. 写的是个ListView 设置 adapter,并在列表末尾显示加载更多,点击 加载更多 时, 变成一个 圆环形的加载动画和 正在加载. 说明下,这个 加载动画是自己做得,一个圆 ...

  7. git常见操作--忽略文件以及常用命令【转】

    转自:http://www.cnblogs.com/elfsundae/archive/2011/07/17/2099698.html References: http://stackoverflow ...

  8. Maven下使用Jetty进行Debug

    1.环境和条件 Maven-3.0.3Eclipse请阅读者事先具备一定maven知识 2 配置2.1 添加Jetty插件 在pom.xml中加入如下代码段 <plugin> <gr ...

  9. SPOJ 1739 Yet Another Equation(Pell方程)

    题目链接:http://www.spoj.com/problems/EQU2/ 题意:给出方程x^2-n*y^2=1的最小整数解. 思路:参见金斌大牛的论文<欧几里得算法的应用>. imp ...

  10. 第八篇 EBS实现企业日常业务运管模型的解决方案设计思路

    常业务运管模型企业有大有小,各行各业,千差万别,但,其日常业务运管也有相通之处,以典型的制造企业为例,其日常业务运管模型如下图所示:       (1)企业日常业务运管模型在市场经济条件下,一个生产型 ...