Entity Framework 4.1 Code First 从两个数据库列映射/填充单个属性
我知道这个问题已经被问过/回答了,但我一生都找不到它。
我正在使用现有数据库创建 ASP.NET MVC 3 应用程序,但想先编写代码,因此我使用 EF Power Tools CTP1 来开始。最终,我将重构一个更好的解决方案,但为了开始,我使用 MVCScaffolding
来生成控制器和视图。我正在努力在模型上创建显示值 (FullName
) 属性,该属性是数据库中 FirstName
和 LastName
列的组合。
我有一个简单的类
public class Manager
{
public Manager(){}
public int ManagerID { get; set; }
[DisplayName] // Not in db.. want to populate this from first & last name
public string FullName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
和一个映射文件,看起来像
public ManagerMap()
{
// Primary Key
this.HasKey(t => t.ManagerID);
// Table & Column Mappings
this.ToTable("Manager");
this.Property(t => t.ManagerID).HasColumnName("ManagerID");
this.Property(t => t.FirstName).HasColumnName("FirstName");
this.Property(t => t.LastName).HasColumnName("LastName");
this.Property(t => t.Email).HasColumnName("Email");
this.Property(t => t.FullName).WhatToDo..? //<-- Can I / how does this mapping look
}
}
是否可以创建 FullName
映射,或者我是否以完全错误的方式处理此问题?
I know this question has been asked/answered but I can't find it for the life of me.
I am creating and ASP.NET MVC 3 app with an existing db but wanted to go code first so I'm used the EF Power Tools CTP1 to get me started. Ultimately, I will refactor to a better solution but to get going I used the MVCScaffolding
to gen up controllers and views. I'm struggling with creating a display value (FullName
) property on my model that is a combination of the FirstName
and LastName
columns in the DB.
I have a simple class
public class Manager
{
public Manager(){}
public int ManagerID { get; set; }
[DisplayName] // Not in db.. want to populate this from first & last name
public string FullName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
and a mapping file that looks like
public ManagerMap()
{
// Primary Key
this.HasKey(t => t.ManagerID);
// Table & Column Mappings
this.ToTable("Manager");
this.Property(t => t.ManagerID).HasColumnName("ManagerID");
this.Property(t => t.FirstName).HasColumnName("FirstName");
this.Property(t => t.LastName).HasColumnName("LastName");
this.Property(t => t.Email).HasColumnName("Email");
this.Property(t => t.FullName).WhatToDo..? //<-- Can I / how does this mapping look
}
}
Is it possible to create the FullName
mapping or am I going about this the entirely wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不可能创建
FullName
映射。但您可以使用这个简单的解决方法:此方法的唯一缺点是您无法在 linq-to-entities 查询中使用
FullName
属性(因此您无法按FullName< 进行排序或搜索) /code> - 您仍必须使用
FirstName
和LastName
)。如果您使用新 Power Tools 的代码生成功能,您实际上应该按照 @Steve Mallory 的建议在单独的部分类中声明此属性 - 如果您想重新生成生成的代码,请不要修改它。
No it is not possible to create
FullName
mapping. But you can use this simple workaround:The only disadvantage of this approach is that you cannot use
FullName
property in linq-to-entities query (so you cannot for example order or search byFullName
- you must still useFirstName
andLastName
).If you use code generation feature of new Power Tools you should really declare this property in separate partial class as proposed by @Steve Mallory - don't modify generated code if you ever want to regenerate it.
我不会映射数据库中的字段。相反,我会声明我的 POCO 是部分的。在另一个文件中,我将具有相同的部分类并在那里定义组合值(仅使用吸气剂)。如果您需要的话,我有示例代码。
如果您稍后进行任何代码生成,将其保存在单独的文件中会有所帮助。
I wouldn't map the field in the database. Instead, I would declare my POCO as partial. In another file, I would have the same partial class and define the combination value there (with only a getter). I have sample code if you need it.
Keeping it in a separate file helps if you do any code generation later.