ef code first例子

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EFCodeFirst.Model
{
    [Table("TUserInfo")]
    public class UserInfo
    {
          [Key,(DatabaseGenerated(DatabaseGeneratedOption.Identity))]
          public int UserId{get;set;}
          public string UserName{get;set;}
          public string UserPwd{get;set;}
    }
}
//实体类

//数据访问,新建一个数据库访问实例
//比如我这里叫UserContext.cs
namespace EFCodeFirst.DAL
{
    public class UserContext:DbContext
    {
        public UserContext():base("name=ConnectString")
        {
            Database.SetInitializer<UserContext>(null);//表示实体对象不初始化数据库
        }
	
	public DbSet<Model.UserInfo> dbUserInfo{get;set;}
    }
}

//剩下就是表对应的dal的操作和bll的操作。


//EF 框架已经搭建完成了,剩下就是开始提供WCF Rest API了
//首先创建一个WCF服务应用程序,创建一个叫UserService的svc文件
//在Global.asax.cs文件中的Application_Start中输入如下映射url路由地址
RouteTable.Routes.Add(new ServiceRote("api/user",new WebServiceHostFactory(),typeof(UserService)));


//IUserService.cs契约接口文件中实例
[OperationContract]
[WebGet(UriTemplate="AddUser?userName={userName}&userPwd={userPwd}",ResponseFormat=WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare)]
int AddUser(string userName,string userPwd);


//其中如果需要开启.net兼容性的话在UserService.svc文件的类开始出加上如下
[AspNetCompatibilityRequirments(RequirmentsMode=AspNetCompatibilityRequirmentsMode.Allowed)]

<system.serviceModel>
    <services>
      <service name="EFCodeFirst.API.UserService" behaviorConfiguration="webHttpendBehavior">
        <endpoint kind="webHttpEndpoint" address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" bindingConfiguration="webHttpConfig" contract="EFCodeFirst.API.UserService"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="webHttpendBehavior">
          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/>
          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpConfig" maxReceivedMessageSize="2147483647" receiveTimeout="00:30:00" sendTimeout="00:30:00" transferMode="Streamed">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
        </binding>
      </webHttpBinding>
    </bindings> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值