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. CentOS下支持exFAT与NTFS

    exFAT: 1.下载fuse-exfat支持软件: exfat支持是通过fuse模块的方式支持的,其项目地址是: https://code.google.com/p/exfat/ ,当前版本是:1. ...

  2. ubuntu 12.04安装vncserver

    1.安装桌面 apt-get install ubuntu-desktop 2.安装vncserver apt-get install vnc4server 3.设置vncserver密码 vncpa ...

  3. Centos上的屏幕保护

    关闭Centos上的屏幕保护:setterm -blank 0 设置Centos上的屏幕保护为5分钟:setterm -blank 5

  4. 浅谈ES5的const以及strict mode

    了解你使用的JavaScript版本是很重要的,因为不同版本的JavaScript对某些语法或者特性的支持情况是不一样的,下面就来举一些例子来说明一下.首先来看一下const关键字,学过比如Java, ...

  5. BZOJ 2115 Xor(抑或值最大路径)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2115 题意:给出一个带权无向图.求一条1到n的路径使得路径上权值的抑或值最大? 思路:( ...

  6. mysql 字符串函数

    对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回NULL. mysq ...

  7. 在服务器端保存ViewState

    1.比较 Asp.net 的一个强大的功能就是ViewState,  但是这也成为很多人诟病的地方,我们先来看一个实例 ViewState其中一个特性就是保存页面的状态,下面我们看一个很简单的登录页面 ...

  8. find-all-anagrams-in-a-string

    https://leetcode.com/problems/find-all-anagrams-in-a-string/ package com.company; import java.util.A ...

  9. ArrayAdapter参数的不同运用

    ArrayAdapter参数不同带来的效果不同 String[] array = {"a","b","c","d",&q ...

  10. hdu1828(线段树+扫描线)

    又知道了线段树的一种用法,除了单点更新,区间更新,还有这种在一段线段上标号但不往下推. 真是神奇 hdu1828 #include <iostream> #include <stdi ...