Is Time Datatype in SQL Server 2008 R2 supports Entityframework with MVC4 - entity-framework

I am using ASP.NET MVC4 with Entityframework.
Does Entityframework supports new Time datatype of SQL.
Thank you

Yes it is supported. If you have table which uses Time SQL type it will be recognized as EDM.Time and generated entity will use TimeSpan as property type. In the same way it works with code first approach. If you map property with TimeSpan type the table will contain SQL Time column.

Related

C# Entity Framework - How to generate object type for input parameters of stored procedure

I have a stored procedure that gets many input parameters (the procedure persoms Insert statement)
I use EF to access this procedure.
Is there a way to automatically generate object type that contains all the input parameters of the procedure?
Something like the complex type that is being generated for the output of the procedure.
My EF version is 6.1.3
No, I don't think it's possible to automatically generate a class for the input parameters of a stored procedure in EF 6.1.3.
This article helped me in working with insert stored procedure with the entity framework:
http://www.entityframeworktutorial.net/EntityFramework5/CRUD-using-stored-procedures.aspx

Entity Framework and Default Date

I have a table in SQL, within that table I have DateTime column with a default value of GetDate()
In entity framework I would like it to use the SQL date time instead of using the local date time of the computer the console app is running on (the SQL server is 1 hour behind).
The column does not allow nulls either, currently it passes in a date value of 1/1/0001 and I get an error:
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated.
Thank you!
Open edmx designer
Select your DateTime column
Go to properties and change StoreGeneratedPattern from None to Computed
That will tell EF not to insert value for that column, thus column will get default value generated by database. Keep in mind that you will not be able to pass some value.
If you're using Fluent API, just add this to your DbContext class:
modelBuilder.Entity<EntityName>()
.Property(p => p.PropertyName)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
Set columnType for that entity in OnModelCreating Method:
modelBuilder.Entity().Property(s => s.ColumnName).HasColumnType("datetime2");

Entity Framework & SQL Compact Edition - how do I get ntext?

The answer to my question should be quite obvious, but I cannot find it. I have a edmx file that has one table. There is a field of type string. EF always generates nvarchar for that (which is kind of expected), but I need an ntext instead of nvarchar for that field as 4000 is too small for me.
So tell me - what is the proper way to tell EF to generate ntext fields?
PS Using Entity Framework 4, SQL CE 3.5
I guess you are using model first, don't you? You can simply create custom T4 template for SQL DDL generation and include logic which will use NTEXT when field is defined with max size.
Default template is on:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\SSDLToSQL10.tt
Just copy this template and find the logic where data type is created. Once you have your template change DDL Generation Template in model properties (in the designer) to your modified version.
There is much more you can do with generation template because you can add some annotations to your model (XML) and use them for custom logic in the SQL generation process.
Just set the property "MaxLength" in the Designer to "Max". This will generate a ntext field in the SQL CE DB.
If your project contains an ADO.Net Entity Data Model (.edmx) then see Ladislav's excellent answer.
But if you're using the Code First libraries and your project doesn't contain a .edmx then you can use the System.ComponentModel.DataAnnotations.ColumnAttribute to specify the column type:
using System.ComponentModel.DataAnnotations;
public class Note {
[Column("Note", TypeName="ntext")]
public string Note { get; set; }
}

Entity Framework 4 maps DateTimeOffset to SQL datetime in Visual Studio 2010

I'm using Visual Studio 2010 RTM with .NET/Entity Framework 4 RTM with a model driven design approach.
When I create an entity with a DateTimeOffset field the EF modeler tries to map the DateTimeOffset to a SQL datetime instead of a SQL datetimeoffset.
I'm using SQL Server 2008 Express so the datetimeoffset is supported in the database.
Visual Studio comes up with this error:
Error 2019: Member Mapping specified is not valid. The type 'Edm.DateTimeOffset[Nullable=False,DefaultValue=,Precision=]' of member 'Created' in type 'Data.SqlStorage.MyType' is not compatible with 'SqlServer.datetime[Nullable=False,DefaultValue=,Precision=3]' of member 'Created' in type 'Data.SqlStorage.Store.MyTypes
If I edit the type directly in the EDMX StorageModels xml section I get the following error:
Error 40: The Type datetimeoffset is not qualified with a namespace or alias. Only PrimitiveTypes can be used without qualification.
Why doesn't the modeler just correctly map this to a SQL datetimeoffset type?
This problem also occured when I was still working with the beta versions of Visual Studio 2010 & .NET framework 4.
In the RTM release, you can do something like this in your DbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<EntityName>().Property(p => p.PropertyName).HasColumnType("datetimeoffset");
}
This is also useful for telling Entity Framework Code First to use datetime2 instead of datetime when generating your database.
Try going the other way (DB->Model). It worked for Julie Lerman. It seems to me your manually-edited EDMX should also work if you qualify the DateTimeOffset with a namespace.
The solution was to update from DB once, AFTER you change manually in the script sql and generated the db. I did it and after I checked my table mapping and the data type was modified to DATE instead of DATETIME. I think the same can be applied if you want to change to DATETIME2.

Entity Framework - Mapping decimal(13,0) problem

I'm migrating the aplication of my company (that nowadays run over SQL Server and Oracle) to ASP NET MVC and Entity Framework for persistence.
A create my Entity Model based on SQL Server Database e separately I create a SSDL for Oracle (for Oracle I use DevArt dotConnect for Oracle Provider) and I get some pain troubles.
My table primary keys are on SQL Server are of type decimal(13,0) and on Oracle are number(13,0) but Oracle map it's type to Int64 and SQL Server to decimal, but I need that SQL Server map it to Int64.
I make these modification manually on Entity Data Model and for create records it's works fine, but when I have to delete or update some record I got these error:
The specified value is not an instance of type 'Edm.Decimal'
Parameter name: value
at System.Data.Common.CommandTrees.DbConstantExpression..ctor(DbCommandTree commandTree, Object value, TypeUsage constantType)
at System.Data.Mapping.Update.Internal.UpdateCompiler.GenerateValueExpression(DbCommandTree commandTree, EdmProperty property, PropagatorResult value)
at System.Data.Mapping.Update.Internal.UpdateCompiler.GenerateEqualityExpression(DbModificationCommandTree commandTree, EdmProperty property, PropagatorResult value)
at System.Data.Mapping.Update.Internal.UpdateCompiler.BuildPredicate(DbModificationCommandTree commandTree, PropagatorResult referenceRow, PropagatorResult current, TableChangeProcessor processor, Boolean& rowMustBeTouched)
at System.Data.Mapping.Update.Internal.UpdateCompiler.BuildDeleteCommand(PropagatorResult oldRow, TableChangeProcessor processor)
at System.Data.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler)
Someone can help me?
Why Entity Framework mapping are so fixed? It could be more flexible?
Ps.: The error that I got, I suspect that is because of a association.
I have a Entity named Province and another named Country and I think that the association between these Entities are causing the problem at update and delete.
Regards,
Douglas Aguiar
This may or may not help you, but i had the same error from doing this same thing. So I edited the Conceptual model and change the primary key field from Int32 to Decimal. So far, seems to have fixed things. I still need to test again against Sql Server and make sure this didnt break it.
I was getting the error "The specified value is not an instance of type 'Edm.Decimal' Parameter name: value" as you posted in your question. I had changed the default data types from Decimal to Int32 as this better reflects the true typing. When I first hit this error I rolled back the type changes and was still getting an exception but it changed just slightly but led to further digging. Bottom line, in my scenario we were expecting a trigger to populate the PK during persistence via Before Insert directive. The problem was that the domain class built by EF was setting the PK at 0 so the trigger was never firing as the incoming PK was not null. Of course EF will not let you set the Entity PK to be nullable. Maybe this will help someone else in the future.