Is there a way to map a DateTimeOffset property to a SQL Server datetime column, with the assumption that you can't change either side, meaning that the property and column have to stay those date types?
I know the easiest is to make them match but want to know if there's a way to work around this. I was looking into custom mappings but it seemed like I had to map all of the columns myself and not just the DateTimeOffset property.
I tried:
modelBuilder.Entity<Customer>().Property(c => c.LastModifiedOn).HasColumnType("datetime");
But that threw the Member Mapping specified is not valid error.
I was hoping to be able to put the UtcDateTime DateTimeOffset property value in the DB and when reading have the DateTimeOffset be in UTC (i.e. have an Offset of zero).
No. DateTimeOffset in .NET class will map to DateTimeOffset SQL type. You cannot change this behavior directly because EF doesn't provide simple type conversions / mappings. If you want to store it as DateTime you must hack it.
First define Customer class with trick to expose private property to mapping referenced by #cincura.net in this post:
public class Customer
{
public static class CustomerExpressions
{
public static readonly Expression<Func<Customer, DateTime>> LastModifiedOn = c => c.LastModifiedOnInternal;
}
// Other properties
public DateTimeOffset LastModifiedOn
{
get { return new DateTimeOffset(LastModifiedOnInternal); }
set { LastModifiedOnInternal = value.DateTime; }
}
private DateTime LastModifiedOnInternal { get; set; }
}
Now you have two properties - one is private and holds DataTime which you want to persist to database and one is public exposing DateTimeOffset for your application. Define it in your context:
public class Context : DbContext
{
public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Ignore(c => c.LastModifiedOn);
modelBuilder.Entity<Customer>().Property(Customer.CustomerExpressions.LastModifiedOn).HasColumnName("LastModifiedOn");
}
}
Anyway why you don't use DateTime directly and store it in UTC?
Related
I'm struggling with using EF6 with DDD principles, namely value objects attached to aggregates. I can't seem to get migrations to generate that reflect the model and I feel like I'm fighting the tooling instead of actually being productive. Given that a NoSQL implementation is probably more appropriate, this is what I'm stuck with.
The first thing that I ran into was the lack of support for interface properties on an EF entity. The work around for that was to add concrete properties to the entity for each of the implementations, but not to the interface. When I implemented the interface, I added logic to return the right one. I had to do this in order to get any migrations to create the properties for the Policies. See Fund.LargestBalanceFirstAllocationPolicy and Fund.PercentageBasedAllocationPolicy This was annoyance one.
The current annoyance and the genesis of the question is the PercentageBasedAllocationPolicy.AllocationValues property. No matter what I do, when running add-migration, I don't get any tables or fields to represent the AllocationValues. This is basically a collection of DDD value objects hanging off of another value object, which hangs off of an aggregate.
I'm convinced that the model and code are correct to do what I want, but EF keeps getting in the way. In MongoDB, when dealing with an interface property, it actually stores the object type in a string so that it knows how to rehydrate the object. I'm considering serializing the problem areas here to a blob and storing it on the object now, which is just as evil...
public interface IFund
{
Guid Id {get;}
string ProperName {get;}
IAllocationPolicy AllocationPolicy{get;}
void ChangeAllocationPolicy(IAllocationPolicy newAllocationPolicy)
}
public class Fund : IFund
{
public Fund()
{
}
public Fund(Guid id, string nickName, string properName)
{
Id = id;
Nickname = nickName;
ProperName = properName;
// This is stupid too, but you have to instantiate these objects inorder to save or you get some EF errors. Make sure the properties on these objects are all defaulted to null.
LargestBalanceFirstAllocationPolicy = new LargestBalanceFirstAllocationPolicy();
PercentageBasedAllocationPolicy = new PercentageBasedAllocationPolicy();
}
public Guid Id { get; private set; }
public string ProperName { get; private set; }
// Do not add this to the interface. It's here for EF reasons only. Do not use internally either. Use the interface implemention of AllocationPolicy instead
public LargestBalanceFirstAllocationPolicy LargestBalanceFirstAllocationPolicy
{
get; private set;
}
// Do not add this to the interface. It's here for EF reasons only. Do not use internally either. Use the interface implemention of AllocationPolicy instead
public PercentageBasedAllocationPolicy PercentageBasedAllocationPolicy
{
get; private set;
}
public void ChangeAllocationPolicy(IAllocationPolicy newAllocationPolicy)
{
if (newAllocationPolicy == null) throw new DomainException("Allocation policy is required");
var allocationPolicy = newAllocationPolicy as PercentageBasedAllocationPolicy;
if (allocationPolicy != null) PercentageBasedAllocationPolicy = allocationPolicy;
var policy = newAllocationPolicy as LargestBalanceFirstAllocationPolicy;
if (policy != null ) LargestBalanceFirstAllocationPolicy = policy;
}
public IAllocationPolicy AllocationPolicy
{
get {
if (LargestBalanceFirstAllocationPolicy != null)
return LargestBalanceFirstAllocationPolicy;
if (PercentageBasedAllocationPolicy != null)
return PercentageBasedAllocationPolicy;
return null;
}
}
}
public interface IAllocationPolicy
{
T Accept<T>(IAllocationPolicyVisitor<T> allocationPolicyVisitor);
}
public class LargestBalanceFirstAllocationPolicy : IAllocationPolicy
{
public T Accept<T>(IAllocationPolicyVisitor<T> allocationPolicyVisitor)
{
return allocationPolicyVisitor.Visit(this);
}
}
[ComplexType]
public class PercentageBasedAllocationPolicy : IAllocationPolicy
{
public PercentageBasedAllocationPolicy()
{
AllocationValues = new List<PercentageAllocationPolicyInfo>();
}
public List<PercentageAllocationPolicyInfo> AllocationValues { get; private set; }
public T Accept<T>(IAllocationPolicyVisitor<T> allocationPolicyVisitor)
{
return allocationPolicyVisitor.Visit(this);
}
}
[ComplexType]
public class PercentageAllocationPolicyInfo
{
public Guid AssetId { get; private set; }
public decimal Percentage { get; private set; }
}
A value type (in EF marked as ComplexType) will never have any tables. The reason being is that a value types are (by definition) really just values. They don't have any Id( otherwise they would be enities) thus you can't create a table for them.
also if i review the requirements for complex type in entity framework https://msdn.microsoft.com/en-us/library/bb738472(v=vs.100).aspx i notice that you can't use inheritance on complex types. Thus if you want to use complex type in your entity framework as you've shown here then you need to make your property a PercentageBasedAllocationPolicy instead of an IAllocationPolicy.
Alternatively you could turn it into an entity with automatic generated keys.
I have been trying to figure out how to set the decimal precision for EF7 (Beta 4) with no luck.
I was expecting to do something like:
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).Precision(10, 6)
This does not appear to be available, but I was able to find the following class in the repository in GitHub:
https://github.com/aspnet/EntityFramework/blob/7.0.0-beta4/src/EntityFramework.Relational/RelationalDecimalTypeMapping.cs
There are no examples of using the RelationalTypeMapping classes or method signatures with them. Maybe this is just used as part of the mapping api for retrieving information?
Another place I might expect this to be is the following:
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().ColumnType()
or
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForSqlServer().ColumnType()
These only takes a string, is this functionality just not implemented yet or am I just not looking in the correct place?
Edit: Just realized that string is probably for .ColumnType("decimal(10,6)") type of solution until this is built out further, still wouldn't mind getting some clarification though as I would prefer not to use strings for this
Edit: after clarification from bricelam I ended up creating the following extension to use for now to avoid using the string, and I appreciate the simplicity of their approach:
public static RelationalPropertyBuilder DecimalPrecision(this RelationalPropertyBuilder propertyBuilder, int precision, int scale)
{
return propertyBuilder.ColumnType($"decimal({precision},{scale})");
}
Usage example:
modelBuilder.Entity<SomeClass>().Property(p => p.DecimalProperty).ForRelational().DecimalPrecision(10,6);
Edit: Making modification for RC1
I haven't tested these out yet, but I just threw together the following 2 samples of what this will probably look like with RC1
public static PropertyBuilder DecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale)
{
return propertyBuilder.HasColumnType($"decimal({precision},{scale})");
}
public static PropertyBuilder SqlDecimalPrecision(this PropertyBuilder propertyBuilder, string precision, string scale)
{
return propertyBuilder.ForSqlServerHasColumnType($"decimal({precision},{scale})");
}
Since I have not yet tried this I am not sure which would be the correct usage between "HasColumnType" or "ForSqlServerHasColumnType", but hopefully this will point someone in the right direction.
Your workaround is the design we intended. Instead of having a bunch of "facets" you can set on a type like precision, scale, max length, unicode/ansi, fixed/variable length, etc. We decided to keep it simple: If the default type mapping isn't what you want, tell us what type to use. There have been talks of going back on this decision and reintroducing the "facets". If you feel strongly about it, I would encourage you to create a new issue.
Also note that there are a bunch of other bugs in type mapping right now, but they should be fixed by the time we release beta5.
The example shown seems to be outdated as per EF RC1.
Here is how I set precision on a decimal field.
Say I have an entity
public class Review
{
public int ReviewId { get; set; }
public decimal TotalScore { get; set; } //I want a precision field in DB
public DateTime CreatedOn { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
}
then in my context class, on model creating, I instantiate the mapping (I could do the mapping there, but I like to keep it separated)
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options ) : base(options)
{
}
public DbSet<Review> Reviews { get; set; }
//etc.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//Mappings
new ReviewMap(modelBuilder.Entity<Review>());
//etc..
}
}
and then the mapping. Remember to use the namespace where the Model extensions are:
using Microsoft.Data.Entity; //here is where the extensions are
public class ReviewMap
{
public ReviewMap(EntityTypeBuilder<Review> entityBuilder)
{
entityBuilder.HasKey(r => r.ReviewId);
//Using the column type extension
entityBuilder.Property(r => r.TotalScore)
.HasColumnType($"decimal(5,2)")
.IsRequired(true);
//and this has nothing to do with the example but it's interesting
//to show how to use Sql command to automatically fulfil a value
//when adding a new Entity
entityBuilder.Property(r => r.CreatedOn)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("GETUTCDATE()")
.IsRequired(true);
}
}
In an existing SQL table a nvarchar column holds enumerative informations.
I now try to use a enum class to map this column using ADO.Net Entity Framework. But somehow this does not seems possible since it requires an integer column.
Does ADO EF not support enums mapped to char columns, or how to realize it allowing linq or lambda syntax on querying?
Error: Given element assignment is invalid.
I am more used to hibernate orm where this is easily possible.
You can use private properties in your model to map your data to whatever property type you want.
// Model
public class Piece
{
// Subclass Piece to add mappings for private properties
public class PieceConfig : EntityTypeConfiguration<Piece>
{
public PieceConfig()
{
Property(b => b.dbtype); // needed for EF to see the private property
}
}
[Column("type", TypeName = "VARCHAR")]
private string dbtype { get; set; }
[NotMapped]
public PIECE type
{
get { return (PIECE)Enum.Parse(typeof(PIECE), dbtype); }
set { dbtype= value.ToString(); }
}
}
Then you just need to add the configuration to your OnModelCreating method
modelBuilder.Configurations.Add(new Piece.PieceConfig());
Say I have an entity as follows:
Restaurant
Id : int
Name : string
CreatedOn : datetime (when it was added to the DB)
and I have a RestaurantService and RestaurantRepository classes. Both of these classes have an AddRestaurant method. The RestaurantService class does some work on the entities and then passes the entities to the repository for persistance.
Which of the two classes should be in charge of setting the CreatedOn property?
Should the repository be in charge of setting the CreatedOn property or should that be part of the Service class responsibility?
I would personally put it as close to the database as possible - including actually in the database, if that meets your needs. Otherwise, though, wherever is closest to the point where it is actually persisted, because that's what is meant by CreatedOn in the first place.
As the record is persisted eventually by the Database itself, it makes sense to instruct the database to generate the current date whenever an entity is added.
You can configure that in Entity without touching the database. For example in Entity Framework 7 (the latest as per RC1)
Say I have an entity
public class Review
{
public int ReviewId { get; set; }
public DateTime CreatedOn { get; set; }
}
then in my context class, on model creating, I instantiate the mapping (I could do the mapping there, but I like to keep it separated)
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options ) : base(options)
{
}
public DbSet<Review> Reviews { get; set; }
//etc.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//Mappings
new ReviewMap(modelBuilder.Entity<Review>());
//etc..
}
}
and then the mapping. (Remember to use the namespace where the Model extensions are). In the instruction below I tell Entity that this value is generated when adding records (entities) and that the application does not need to manually set the datetime. Instead I tell it to execute the SQL Server function GETUTCDATE() when adding an entity so I don't need to worry about that anymore.
using Microsoft.Data.Entity; //here is where the extensions are
public class ReviewMap
{
public ReviewMap(EntityTypeBuilder<Review> entityBuilder)
{
entityBuilder.HasKey(r => r.ReviewId);
entityBuilder.Property(r => r.CreatedOn)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("GETUTCDATE()")
.IsRequired(true);
}
}
About your concern about database time zone, the good practice is to ALWAYS save dates as UTC in database (location agnostic) and transform the date as per the user location so the user always sees local date. That's why the instruction for the database is GETUTCDATE() rather than GETDATE()
I am using EF Code first with database first approach.
"with Database.SetInitializer(null);"
My table has two columns createddate and amendddate. They are managed by SQL Server using triggers. The idea is that when data entry happens then these columns gets data via triggers.
Now What I want to do is to make this read only from EF Code first point of view. I.e. I want to be able to see the createddate and ameneded dates from my app but I dont want to amend these data.
I have tried using private modifiers on setter but no luck.When I try to add new data to the table it tried to enter DateTime.Max date to the database which throws error from SQL server.
Any idea?
You cannot use private modifiers because EF itself needs to set your properties when it is loading your entity and Code First can only do this when a property has public setter (in contrast to EDMX where private setters are possible (1), (2)).
What you need to do is mark your for CreatedDate with DatabaseGeneratedOption.Identity and your AmendDate with DatabaseGeneratedOption.Computed. That will allow EF to correctly load data from the database, reload data after insert or update so that entity is up to date in your application and at the same time it will not allow you to change the value in the application because the value set in the application will never be passed to the database. From an object oriented perspective it is not a very nice solution but from the functionality perspective it is exactly what you want.
You can do it either with data annotations:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime CreatedDate { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime AmendDate { get; set; }
Or with fluent API in OnModelCreating override in your derived context:
modelBuilder.Entity<YourEntity>()
.Property(e => e.CreatedDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<YourEntity>()
.Property(e => e.AmendDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
EF core 1.1 or later versions yes you can use read only property in poco classes. What you need to do is using backing-field.
public class Blog
{
private string _validatedUrl;
public int BlogId { get; set; }
public string Url
{
get { return _validatedUrl; }
}
public void SetUrl(string url)
{
using (var client = new HttpClient())
{
var response = client.GetAsync(url).Result;
response.EnsureSuccessStatusCode();
}
_validatedUrl = url;
}
}
class MyContext : DbContext
{
public DbSet Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.HasField("_validatedUrl");
}
}
and fluent api...
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.HasField("_validatedUrl")
.UsePropertyAccessMode(PropertyAccessMode.Field);
Take a look here..