Answered by:
EF Code First Custom Schema Mapping

Question
-
I was following Scott Guthrie's blog on custom schema mapping (http://weblogs.asp.net/scottgu/archive/2010/07/23/entity-framework-4-code-first-custom-database-schema-mapping.aspx) and it appears that MapSingleType is no longer used (http://social.msdn.microsoft.com/Forums/eu/adonetefx/thread/66ea55e0-b3cb-41be-a8c0-b22882a9e993). So I'm trying to figure out how to map columns and table name to my entity. It appears there isn't any change to how table names are mapped:
'map relief to existing table modelBuilder.Entity(Of Relief)().ToTable("tbl_Relief")
So far all my searches are from a CTP release where MapSingleType was still used. Assuming all columns in the DB have a "col_" prefix, how would I map to my entity?
col_relief_id -> reliefid
col_relief_name -> reliefname
Friday, October 7, 2011 5:25 PM
Answers
-
I might have the answer. Is this correct?
Protected Overrides Sub OnModelCreating(modelBuilder As System.Data.Entity.DbModelBuilder) modelBuilder.Configurations.Add(New ReliefConfiguration) 'map relief to existing table modelBuilder.Entity(Of Relief)().ToTable("tbl_Relief") End Sub Public Class ReliefConfiguration Inherits EntityTypeConfiguration(Of Relief) Public Sub New() [Property](Function(r) r.reliefid).HasColumnName("col_relief_id") [Property](Function(r) r.reliefname).HasColumnName("col_relief_name") End Sub End Class
Friday, October 7, 2011 6:43 PM -
Hi pretzelb,
Welcome!
Right! This's Fluent API in Code First: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx
You can use try to use Data Annotation to decorate the properties: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Proposed as answer by Giorgi Zautashvili Monday, October 10, 2011 11:49 AM
- Marked as answer by Alan_chen Tuesday, October 18, 2011 1:58 AM
Monday, October 10, 2011 1:44 AM
All replies
-
I might have the answer. Is this correct?
Protected Overrides Sub OnModelCreating(modelBuilder As System.Data.Entity.DbModelBuilder) modelBuilder.Configurations.Add(New ReliefConfiguration) 'map relief to existing table modelBuilder.Entity(Of Relief)().ToTable("tbl_Relief") End Sub Public Class ReliefConfiguration Inherits EntityTypeConfiguration(Of Relief) Public Sub New() [Property](Function(r) r.reliefid).HasColumnName("col_relief_id") [Property](Function(r) r.reliefname).HasColumnName("col_relief_name") End Sub End Class
Friday, October 7, 2011 6:43 PM -
Hi pretzelb,
Welcome!
Right! This's Fluent API in Code First: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx
You can use try to use Data Annotation to decorate the properties: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Proposed as answer by Giorgi Zautashvili Monday, October 10, 2011 11:49 AM
- Marked as answer by Alan_chen Tuesday, October 18, 2011 1:58 AM
Monday, October 10, 2011 1:44 AM -
Hi,
I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?
If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Friday, October 14, 2011 1:36 AM