Sha*_*ica 3 c# entity-framework entity-framework-5
我在我的Entity Framework 5模型中定义了一个枚举,我用它来定义表格中字段的类型,例如
public enum PrivacyLevel : byte {
Public = 1,
FriendsOnly = 2,
Private = 3,
}
Run Code Online (Sandbox Code Playgroud)
我有一个表Publication有一个tinyint字段PrivacyLevel,我已经在EF模型中映射使用PrivacyLevel上面定义的类型,使用此处描述的方法.
但我也希望能够为枚举的每个值显示字符串描述.这是我过去为枚举做的,通过使用Description属性进行装饰,例如
public enum PrivacyLevel : byte {
[Description("Visible to everyone")]
Public = 1,
[Description("Only friends can view")]
FriendsOnly = 2,
[Description("Only I can view")]
Private = 3,
}
Run Code Online (Sandbox Code Playgroud)
我有一些代码通过检查它们是否具有Description属性将枚举转换为字符串,并且运行良好.但是在这里,因为我必须在我的模型中定义枚举,底层代码是自动生成的,并且我没有任何稳定来装饰它们.
任何解决方法的想法?
Pat*_*gee 14
不确定这是否是你所追求的,但从我的理解,我会尽量清楚,因为你有一个具体的数据库第一种方法,你可以使用Dto方法通过AutoMapper将你的实体模型大部分抽象到ViewModels.
使用自动配置文件,您可以快速设置各种环境和方案的配置文件,以实现灵活性和适应性
所以这就是这个"Enum"导致我出现问题

这是我对这个枚举的观点模型

首先是我的布局

这里是Account实体到Account的viewmodel的简单映射
public class AccountProfile : Profile
{
protected override void Configure()
{
// Map from Entity object to a View Model we need or use
// AutoMapper will automatically map any names that match it's conventions, ie properties from Entity to ViewModel have exact same name properties
Mapper.CreateMap<Account, AccountViewModel>()
.ForMember(model => model.CurrentPrivacy, opt => opt.MapFrom(account => (PrivacyLevelViewModel)account.PrivacyLevel));
Mapper.CreateMap<Account, EditAccountViewModel>()
.ForMember(model => model.SelectedPrivacyLevel, opt => opt.MapFrom(account => (PrivacyLevelViewModel) account.PrivacyLevel));
// From our View Model Changes back to our entity
Mapper.CreateMap<EditAccountViewModel, Account>()
.ForMember(entity => entity.Id, opt => opt.Ignore()) // We dont change id's
.ForMember(entity => entity.PrivacyLevel, opt => opt.MapFrom(viewModel => (PrivacyLevel)viewModel.NewSelectedPrivacyLevel));
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这不必适用于MVC,这可以在WPF或其他不依赖于Web的应用程序中使用,但由于这是一种很好的解释方式,因此我将MVC用于此示例.
当我第一次获得我的配置文件的Http Get请求时,我从数据库中获取实体并将实际需要的任何内容映射到视图
public ActionResult Index()
{
// Retrieve account from db
var account = new Account() { Id = 1, Name = "Patrick", AboutMe = "I'm just another dude", ProfilePictureUrl = "", PrivacyLevel = PrivacyLevel.Private, Friends = new Collection<Account>() };
// ViewModel abstracts the Entities and ensures behavour that only matters to the UI
var accountViewModel = Mapper.Map<AccountViewModel>(account);
return View(accountViewModel); // strongly typed view model
}
Run Code Online (Sandbox Code Playgroud)
所以我的个人资料索引视图可以使用我的枚举视图模型

这是输出

现在,当我想更改我的隐私设置时,我可以创建一个新的EditAccountViewModel,它允许我在下拉列表中提交新值
public class EditAccountViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string AboutMe { get; set; }
public int NewSelectedPrivacyLevel { get; set; }
public PrivacyLevelViewModel SelectedPrivacyLevel { get; set; }
public SelectList PrivacyLevels
{
get
{
var items = Enum.GetValues(typeof (PrivacyLevelViewModel))
.Cast<PrivacyLevelViewModel>()
.Select(viewModel => new PrivacyLevelSelectItemViewModel()
{
Text = viewModel.DescriptionAttr(),
Value = (int)viewModel,
});
//SelectPrivacyLevel was mapped by AutoMapper in the profile from
//original entity value to this viewmodel
return new SelectList(items, "Value", "Text", (int) SelectedPrivacyLevel);
}
}
}
Run Code Online (Sandbox Code Playgroud)

现在,一旦我发送了我的新更改值的帖子,有趣的部分是如何使用更新的隐私设置从数据库修改"真实"实体
在将表单提交回我的编辑操作时,您可以获取原始的实际数据库实体,然后在ViewModel状态有效时合并更改

AutoMapper允许您配置ViewModel如何映射到实体,如果某些属性应该更改,从整数实体到视图模型的字符串值,也许您希望枚举真的是"视图"中的字符串,只有enum用于db,使用自动映射器,它允许您配置所有这些风格,并且通过约定,如果您的视图模型具有相同的属性名称/ camel case大写,则不需要配置"每个属性".

最后,在使用这些配置文件之前,必须在应用程序入口点加载它们,如global.asax或Main.
AutoMapper只需要"配置"一次即可加载应用程序中定义的任何类型的配置文件.通过一些反射,您可以使用以下代码加载程序集中的所有配置文件:
public class AutoMapperConfig
{
public static void RegisterConfig()
{
Mapper.Initialize(config => GetConfiguration(Mapper.Configuration));
}
private static void GetConfiguration(IConfiguration configuration)
{
configuration.AllowNullDestinationValues = true;
configuration.AllowNullCollections = true;
IEnumerable<Type> profiles = Assembly.GetExecutingAssembly().GetTypes().Where(type => typeof(Profile).IsAssignableFrom(type));
foreach (var profile in profiles)
{
configuration.AddProfile(Activator.CreateInstance(profile) as Profile);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在global.asax中调用配置:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
AutoMapperConfig.RegisterConfig(); // AutoMapperConfig.cs
}
Run Code Online (Sandbox Code Playgroud)
有关如何使用AutoMapper及其如何使您受益的更多信息,请访问: