How to map database column to EF model property in a more efficient way than I'm doing now
up vote
1
down vote
favorite
I have a nullable varchar(max) column in SQL Server that I'm mapping to a Guid? in EF code-first. However, this property is actually in a base class that many other entities derive from.
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Model1>().Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
The above line is repeated many times for each table. Is there a way to tell EF that this is a base class property so the mapping can be declared only once?
c#
add a comment |
up vote
1
down vote
favorite
I have a nullable varchar(max) column in SQL Server that I'm mapping to a Guid? in EF code-first. However, this property is actually in a base class that many other entities derive from.
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Model1>().Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
The above line is repeated many times for each table. Is there a way to tell EF that this is a base class property so the mapping can be declared only once?
c#
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a nullable varchar(max) column in SQL Server that I'm mapping to a Guid? in EF code-first. However, this property is actually in a base class that many other entities derive from.
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Model1>().Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
The above line is repeated many times for each table. Is there a way to tell EF that this is a base class property so the mapping can be declared only once?
c#
I have a nullable varchar(max) column in SQL Server that I'm mapping to a Guid? in EF code-first. However, this property is actually in a base class that many other entities derive from.
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Model1>().Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
The above line is repeated many times for each table. Is there a way to tell EF that this is a base class property so the mapping can be declared only once?
c#
c#
edited Nov 11 at 10:11
Ivan Stoev
97.1k765119
97.1k765119
asked Nov 11 at 5:17
user246392
1,35482953
1,35482953
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Sure it is possible. With the lack of custom conventions, it is achieved with the "typical" modelBuilder.Model.GetEntityTypes() loop. Something like this (just change the base class and the property names):
var entityTypes = modelBuilder.Model.GetEntityTypes()
.Where(t => t.ClrType.IsSubclassOf(typeof(BaseClass)));
var valueConverter = new ValueConverter<Guid, string>(
v => v.ToString(), v => (Guid?)Guid.Parse(v));
foreach (var entityType in entityTypes)
entityType.FindProperty(nameof(BaseClass.Property1)).SetValueConverter(valueConverter);
You may also consider using the EF Core provided out of the box Guid to String converter:
var valueConverter = new GuidToStringConverter();
add a comment |
up vote
0
down vote
Better to make next calculation property:
[Column("Property1")]
public string Property1Raw get; set;
[IgnoreDataMember]
public Guid? Property1
get => Guid.TryParse(Property1AsString, out Guid result) ? result : (Guid?)null;
set => Property1Raw = value?.ToString();
add a comment |
up vote
0
down vote
Another way to do is it to have a matching base class IEntityTypeConfiguration:
internal class EntityConfiguration<T> : IEntityTypeConfiguration<T> where T : Entity
public virtual void Configure(EntityTypeBuilder<T> builder)
builder.Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
// ... Other base-specific config here
(Assuming here your base class is called Entity - change as needed).
This works better when you use the pattern of factoring out your entity configurations, so yours might be like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new Model1EntityConfiguration());
modelBuilder.ApplyConfiguration(new Model2EntityConfiguration());
// ...
...
internal sealed class Model1EntityConfiguration : EntityConfiguration<Model1>
public override void Configure(EntityTypeBuilder<Model1> builder)
base.Configure(builder); // <-- here's the key bit
// ...; e.g.
builder.Property(c => c.Name).HasMaxLength(80).IsRequired();
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Sure it is possible. With the lack of custom conventions, it is achieved with the "typical" modelBuilder.Model.GetEntityTypes() loop. Something like this (just change the base class and the property names):
var entityTypes = modelBuilder.Model.GetEntityTypes()
.Where(t => t.ClrType.IsSubclassOf(typeof(BaseClass)));
var valueConverter = new ValueConverter<Guid, string>(
v => v.ToString(), v => (Guid?)Guid.Parse(v));
foreach (var entityType in entityTypes)
entityType.FindProperty(nameof(BaseClass.Property1)).SetValueConverter(valueConverter);
You may also consider using the EF Core provided out of the box Guid to String converter:
var valueConverter = new GuidToStringConverter();
add a comment |
up vote
2
down vote
accepted
Sure it is possible. With the lack of custom conventions, it is achieved with the "typical" modelBuilder.Model.GetEntityTypes() loop. Something like this (just change the base class and the property names):
var entityTypes = modelBuilder.Model.GetEntityTypes()
.Where(t => t.ClrType.IsSubclassOf(typeof(BaseClass)));
var valueConverter = new ValueConverter<Guid, string>(
v => v.ToString(), v => (Guid?)Guid.Parse(v));
foreach (var entityType in entityTypes)
entityType.FindProperty(nameof(BaseClass.Property1)).SetValueConverter(valueConverter);
You may also consider using the EF Core provided out of the box Guid to String converter:
var valueConverter = new GuidToStringConverter();
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Sure it is possible. With the lack of custom conventions, it is achieved with the "typical" modelBuilder.Model.GetEntityTypes() loop. Something like this (just change the base class and the property names):
var entityTypes = modelBuilder.Model.GetEntityTypes()
.Where(t => t.ClrType.IsSubclassOf(typeof(BaseClass)));
var valueConverter = new ValueConverter<Guid, string>(
v => v.ToString(), v => (Guid?)Guid.Parse(v));
foreach (var entityType in entityTypes)
entityType.FindProperty(nameof(BaseClass.Property1)).SetValueConverter(valueConverter);
You may also consider using the EF Core provided out of the box Guid to String converter:
var valueConverter = new GuidToStringConverter();
Sure it is possible. With the lack of custom conventions, it is achieved with the "typical" modelBuilder.Model.GetEntityTypes() loop. Something like this (just change the base class and the property names):
var entityTypes = modelBuilder.Model.GetEntityTypes()
.Where(t => t.ClrType.IsSubclassOf(typeof(BaseClass)));
var valueConverter = new ValueConverter<Guid, string>(
v => v.ToString(), v => (Guid?)Guid.Parse(v));
foreach (var entityType in entityTypes)
entityType.FindProperty(nameof(BaseClass.Property1)).SetValueConverter(valueConverter);
You may also consider using the EF Core provided out of the box Guid to String converter:
var valueConverter = new GuidToStringConverter();
answered Nov 11 at 10:09
Ivan Stoev
97.1k765119
97.1k765119
add a comment |
add a comment |
up vote
0
down vote
Better to make next calculation property:
[Column("Property1")]
public string Property1Raw get; set;
[IgnoreDataMember]
public Guid? Property1
get => Guid.TryParse(Property1AsString, out Guid result) ? result : (Guid?)null;
set => Property1Raw = value?.ToString();
add a comment |
up vote
0
down vote
Better to make next calculation property:
[Column("Property1")]
public string Property1Raw get; set;
[IgnoreDataMember]
public Guid? Property1
get => Guid.TryParse(Property1AsString, out Guid result) ? result : (Guid?)null;
set => Property1Raw = value?.ToString();
add a comment |
up vote
0
down vote
up vote
0
down vote
Better to make next calculation property:
[Column("Property1")]
public string Property1Raw get; set;
[IgnoreDataMember]
public Guid? Property1
get => Guid.TryParse(Property1AsString, out Guid result) ? result : (Guid?)null;
set => Property1Raw = value?.ToString();
Better to make next calculation property:
[Column("Property1")]
public string Property1Raw get; set;
[IgnoreDataMember]
public Guid? Property1
get => Guid.TryParse(Property1AsString, out Guid result) ? result : (Guid?)null;
set => Property1Raw = value?.ToString();
answered Nov 11 at 10:27
Sergey Shulik
678824
678824
add a comment |
add a comment |
up vote
0
down vote
Another way to do is it to have a matching base class IEntityTypeConfiguration:
internal class EntityConfiguration<T> : IEntityTypeConfiguration<T> where T : Entity
public virtual void Configure(EntityTypeBuilder<T> builder)
builder.Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
// ... Other base-specific config here
(Assuming here your base class is called Entity - change as needed).
This works better when you use the pattern of factoring out your entity configurations, so yours might be like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new Model1EntityConfiguration());
modelBuilder.ApplyConfiguration(new Model2EntityConfiguration());
// ...
...
internal sealed class Model1EntityConfiguration : EntityConfiguration<Model1>
public override void Configure(EntityTypeBuilder<Model1> builder)
base.Configure(builder); // <-- here's the key bit
// ...; e.g.
builder.Property(c => c.Name).HasMaxLength(80).IsRequired();
add a comment |
up vote
0
down vote
Another way to do is it to have a matching base class IEntityTypeConfiguration:
internal class EntityConfiguration<T> : IEntityTypeConfiguration<T> where T : Entity
public virtual void Configure(EntityTypeBuilder<T> builder)
builder.Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
// ... Other base-specific config here
(Assuming here your base class is called Entity - change as needed).
This works better when you use the pattern of factoring out your entity configurations, so yours might be like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new Model1EntityConfiguration());
modelBuilder.ApplyConfiguration(new Model2EntityConfiguration());
// ...
...
internal sealed class Model1EntityConfiguration : EntityConfiguration<Model1>
public override void Configure(EntityTypeBuilder<Model1> builder)
base.Configure(builder); // <-- here's the key bit
// ...; e.g.
builder.Property(c => c.Name).HasMaxLength(80).IsRequired();
add a comment |
up vote
0
down vote
up vote
0
down vote
Another way to do is it to have a matching base class IEntityTypeConfiguration:
internal class EntityConfiguration<T> : IEntityTypeConfiguration<T> where T : Entity
public virtual void Configure(EntityTypeBuilder<T> builder)
builder.Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
// ... Other base-specific config here
(Assuming here your base class is called Entity - change as needed).
This works better when you use the pattern of factoring out your entity configurations, so yours might be like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new Model1EntityConfiguration());
modelBuilder.ApplyConfiguration(new Model2EntityConfiguration());
// ...
...
internal sealed class Model1EntityConfiguration : EntityConfiguration<Model1>
public override void Configure(EntityTypeBuilder<Model1> builder)
base.Configure(builder); // <-- here's the key bit
// ...; e.g.
builder.Property(c => c.Name).HasMaxLength(80).IsRequired();
Another way to do is it to have a matching base class IEntityTypeConfiguration:
internal class EntityConfiguration<T> : IEntityTypeConfiguration<T> where T : Entity
public virtual void Configure(EntityTypeBuilder<T> builder)
builder.Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p));
// ... Other base-specific config here
(Assuming here your base class is called Entity - change as needed).
This works better when you use the pattern of factoring out your entity configurations, so yours might be like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new Model1EntityConfiguration());
modelBuilder.ApplyConfiguration(new Model2EntityConfiguration());
// ...
...
internal sealed class Model1EntityConfiguration : EntityConfiguration<Model1>
public override void Configure(EntityTypeBuilder<Model1> builder)
base.Configure(builder); // <-- here's the key bit
// ...; e.g.
builder.Property(c => c.Name).HasMaxLength(80).IsRequired();
answered Nov 11 at 10:32
sellotape
5,50221619
5,50221619
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246043%2fhow-to-map-database-column-to-ef-model-property-in-a-more-efficient-way-than-im%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown