odata.EF一些常用配置

Enable-Migrations //在数据库里建一个表
Add-Migration //添加
Update-Database //更新数据
install-package entityframework //添加最新版的EF
Install-Package Microsoft.AspNet.Odata //添加最新版的Odata
Install-Package Jquery //添加最新版的Jquery


在EF类里面添加主外键

        [Table("Users")] //创建一个表,表名为Users
        public class SUsers
        {
            [Key] //唯一
            public int Ids { get; set; }

            [Required] //非空
            public string Uname { get; set; }
            public string Password { get; set; }
            public string Nname { get; set; }
            public string Sex { get; set; }
            public int NationCode { get; set; }
            public DateTimeOffset Birthday { get; set; } //DateTimeOffset就是DateTime类型

            [ForeignKey("NationCode")] //定义一个外键,外键为NationCode
            public virtual Nation Nation { get; set; } //外键
        }
        public class Nation
        {
            [Key] //唯一
            public int Ids { get; set; }
            public string NationName { get; set; }
            public virtual ICollection<SUsers> SUsers { get; set; }  //主键

        }
    }
//配置Web.config
  <connectionStrings>
    <add name="UsersContext" connectionString="Data Source=.;Initial Catalog=User;Persist Security Info=True;User ID=sa;Password=123;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>
 //配置Odata
  config.Count().Select().OrderBy().Filter().Expand().MaxTop(null);
            ODataModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Users>("Users");
            config.MapODataServiceRoute(
                routeName: "odata",
                routePrefix: "odata",
                model: builder.GetEdmModel());

  

posted @ 2018-06-12 15:10  氏张  阅读(191)  评论(0编辑  收藏  举报