Asp.net Webform 使用Repository模式实现CRUD操作代码

作者:青岛澄润国际贸易有限公司 来源:www.usabcc.com 发布时间:2017-09-09 10:22:24
Asp.net Webform 使用Repository模式实现CRUD操作代码生成工具 新建一个Product实体类

public class Product

{

[Key]

public int Id { get; set; }

[Display( Name="商品编码"),MaxLength(10),Required]

public string SKU { get; set; }

[Display(Name = "品名"), MaxLength(50), Required]

public string Name { get; set; }

[Display(Name = "规格"), MaxLength(20)]

public string Model { get; set; }

[Display(Name = "单位"), MaxLength(10)]

public string Unit { get; set; }

[Display(Name = "单价")]

public decimal Price { get; set; }

[Display(Name = "数量")]

public int Qty { get; set; }

}

一个Company实体类+Department实体类,一对多的关系

public class Company

{

public Company()

{

Departments = new HashSet<Department>();

}

[Key, Display(Name = "系统编码")]

public int Id { get; set; }

[Display(Name="公司名称"), MaxLength(10), Required]

public string Name { get;set;}

[Display(Name = "地址"), MaxLength(50)]

public string Address { get; set; }

[Display(Name = "联系人"), MaxLength(10)]

public string ContactName { get; set; }

[Display(Name = "联系电话"), MaxLength(20)]

public string ContactPhone { get; set; }

public virtual ICollection<Department> Departments { get; set; }

}

public class Department

{

public Department()

{

}

[Key,Display(Name = "系统编码")]

public int Id { get; set; }

[Display(Name="部门名称"),Required,MaxLength(20)]

public string Name { get; set; }

public int Company_Id{get;set;}

[ForeignKey("Company_Id")]

public Company Company { get; set; }

}

实体类字段上的验证比如Required,Display会在生成的Form和Gridview中体现,包括负责的正则表达验证都可以实现。

修改App Start目录下RouteConfig.cs

public static class RouteConfig

{

public static void RegisterRoutes(RouteCollection routes)

{

var settings = new FriendlyUrlSettings();

settings.AutoRedirectMode = RedirectMode.Permanent;

routes.EnableFriendlyUrls(settings);

var DefaultModel = new MetaModel(true);

DefaultModel.RegisterContext(

new Microsoft.AspNet.DynamicData.ModelProviders.EFDataModelProvider(() => new WebAPP.Models.MyDbContext()),

new ContextConfiguration { ScaffoldAllTables = true });

}

}

用于GridView绑定外键字段列时,系统自动判断使用ForeignKey 动态字段类型

,站群软件

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:咸宁SEO http://xianning.4567w.com

  • 上一篇:js获取元素transform参数得出的个人理解
  • 下一篇:最后一页
  •