当我开始使用.NET Webforms时,我找不到要跟随的文件夹结构,因为VS提供了像"App_Code"这样的应用程序文件夹,大多数应用程序示例都在其中放置了"BLL","DAL"等等.
但是现在在MVC中,我检查的每个例子都使用不同的结构,这次没有标准,我没有在Google或SO上找到一个好的解决方案.
所以,也许我们可以分享我们如何组织我们的MVC项目,可以帮助其他人做出自己的想法.这是我使用的中小型项目的结构:
App_Data
Areas
Admin
Controllers
Models
Views
MyAccount
Controllers
Models
Views
Content
Images
Scripts
Styles
Controllers
HomeController.cs
Helpers
ExtensionMethods // I.e. based on HtmlHelper, use "helper" suffix
MenuHelper.cs // to be called as html.Menu()
Utilities.cs // Other generic (static) libraries, no suffix used
Models
ViewModels // for passing models to Views
RegisterViewModel.cs // use "ViewModel" suffix
Customer.cs // to extend models like adding Model Validation
Repositories
CustomerRepository.cs // use "Repository" suffix
Services
CustomerService.cs // use "Service" suffix, …Run Code Online (Sandbox Code Playgroud) asp.net-mvc conventions naming-conventions asp.net-mvc-3 asp.net-mvc-2
我正在使用单数表名称的标准.默认情况下,EF4 Code First具有复数表名.我已经把代码覆盖了这个约定,但似乎没有用.
使用部分:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Database;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Conventions.Edm;
Run Code Online (Sandbox Code Playgroud)
数据背景:
public class SiteDataContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<BlogFeedback> BlogFeedbacks { get; set; }
public DbSet<BlogCategory> BlogCategories { get; set; }
// Twist our database
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
base.OnModelCreating(modelBuilder);
}
}
Run Code Online (Sandbox Code Playgroud)
创建的表:
因为约定覆盖(以及我需要的)应该是:
任何人都知道为什么覆盖线不起作用?非常感谢.
我有以下型号:
public class Blog
{
public int BlogID { get; set; }
public int CategoryID { get; set; }
[MaxLength(70)]
[Required]
public string BlogTitle { get; set; }
[Column(TypeName="ntext")]
public string BlogContent { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已手动将字段设置为数据库中BlogContent的ntext类型(16字节)SQL CE4.
但是,每次我尝试插入超过4000个字符的文本时,都会出现以下错误:
一个或多个实体的验证失败.有关详细信息,请参阅"EntityValidationErrors"属性
我试过设置注释[Column(TypeName="ntext")],但这没有区别.当我循环EntityValidationErrors收集时,问题是由BlogContent错误引起的:
字符串不能超过4000个字符
如何定义我的模型以获得ntext字段BlogContent?
似乎忽略了任何数据注释; 假设MaxLength默认情况下,no的字符串限制为4000个字符.
我有一个ViewModel,它包含一个Model和一些额外的属性.模型和属性上有验证,但执行时,仅检查模型上的验证,忽略属性中的验证.
该模型:
[MetadataType(typeof(Customer_Validation))]
public partial class Customer
{
}
public class Customer_Validation
{
[Required(ErrorMessage="Please enter your First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter your Last name")]
public string LastName { get; set; }
[Required(ErrorMessage = "Sorry, e-mail cannot be empty")]
[Email(ErrorMessage="Invalid e-mail")]
public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
ViewModel
public class RegisterViewModel
{
public Customer NewCustomer { get; private set; }
[Required(ErrorMessage="Required")]
public string Password { get; private set; }
public RegisterViewModel(Customer …Run Code Online (Sandbox Code Playgroud) 问题
需要使用EF4 + SQL CE4将int转换为字符串.使用SqlFunctions.StringConvert(double)的推荐选项仍然给我错误.
选项1(原件).这给了我错误:
public static IEnumerable<SelectListItem> xxGetCustomerList()
{
using (DatabaseEntities db = new DatabaseEntities())
{
var list = from l in db.Customers
orderby l.CompanyName
select new SelectListItem { Value = l.CustomerID.ToString(), Text = l.CompanyName };
return list.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
选项2(大多数建议).然后很多帖子建议,我使用库System.Data.Objects.SqlClient中的SqlFunctions.StringConvert()函数:
public static IEnumerable<SelectListItem> GetCustomerList()
{
using (DatabaseEntities db = new DatabaseEntities())
{
var list = from l in db.Customers
orderby l.CompanyName
select new SelectListItem { Value = SqlFunctions.StringConvert((double)l.CustomerID), Text = l.CompanyName };
return list.ToList();
} …Run Code Online (Sandbox Code Playgroud)