Quantcast
Channel: Entity Framework
Viewing all 10318 articles
Browse latest View live

Edited Unassigned: SqlServer Compact and Code First DataAnnotations Issue [2585]

$
0
0
Hi,

I've found a following bug when you use EntityFramework.SqlServerCompact provider:

If you wants to have nchar(n) in the database and tries to use Code First DataAnnotations to describe a property - app fails either to create database or read data from existing one

```
[StringLength(3)]
[Column(TypeName = "char")]
public string ThreeLetterISOName { get; set; }
```
It works perfectly on SQL LocalDB.

As a workaround I've used the Fluent API. First of all you have to remove [Column(TypeName = "char")] from your entity property:

```
[StringLength(3)]
public string ThreeLetterISOName { get; set; }
```
and in context class I've added:

```
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Language>().Property(t => t.ThreeLetterISOName).IsFixedLength();
}
```
This way it works with Sql Server Compact.

I've used EntityFramework 6.1.1 and EntityFramework.SqlServerCompact 6.1.1

Commented Unassigned: SqlServer Compact and Code First DataAnnotations Issue [2585]

$
0
0
Hi,

I've found a following bug when you use EntityFramework.SqlServerCompact provider:

If you wants to have nchar(n) in the database and tries to use Code First DataAnnotations to describe a property - app fails either to create database or read data from existing one

```
[StringLength(3)]
[Column(TypeName = "char")]
public string ThreeLetterISOName { get; set; }
```
It works perfectly on SQL LocalDB.

As a workaround I've used the Fluent API. First of all you have to remove [Column(TypeName = "char")] from your entity property:

```
[StringLength(3)]
public string ThreeLetterISOName { get; set; }
```
and in context class I've added:

```
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Language>().Property(t => t.ThreeLetterISOName).IsFixedLength();
}
```
This way it works with Sql Server Compact.

I've used EntityFramework 6.1.1 and EntityFramework.SqlServerCompact 6.1.1
Comments: SQL Server Compact only has unicode (nchar/nvarcher) data types. Have you tried with [Column(TypeName = "nchar")]

Commented Unassigned: SqlServer Compact and Code First DataAnnotations Issue [2585]

$
0
0
Hi,

I've found a following bug when you use EntityFramework.SqlServerCompact provider:

If you wants to have nchar(n) in the database and tries to use Code First DataAnnotations to describe a property - app fails either to create database or read data from existing one

```
[StringLength(3)]
[Column(TypeName = "char")]
public string ThreeLetterISOName { get; set; }
```
It works perfectly on SQL LocalDB.

As a workaround I've used the Fluent API. First of all you have to remove [Column(TypeName = "char")] from your entity property:

```
[StringLength(3)]
public string ThreeLetterISOName { get; set; }
```
and in context class I've added:

```
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Language>().Property(t => t.ThreeLetterISOName).IsFixedLength();
}
```
This way it works with Sql Server Compact.

I've used EntityFramework 6.1.1 and EntityFramework.SqlServerCompact 6.1.1
Comments: ErikEJ, you 100% are right. I didn't noticed that initially. With __nchar__ it works as expected. So ticket can be closed. Thanks for turning me into right direction!

Commented Unassigned: SqlServer Compact and Code First DataAnnotations Issue [2585]

$
0
0
Hi,

I've found a following bug when you use EntityFramework.SqlServerCompact provider:

If you wants to have nchar(n) in the database and tries to use Code First DataAnnotations to describe a property - app fails either to create database or read data from existing one

```
[StringLength(3)]
[Column(TypeName = "char")]
public string ThreeLetterISOName { get; set; }
```
It works perfectly on SQL LocalDB.

As a workaround I've used the Fluent API. First of all you have to remove [Column(TypeName = "char")] from your entity property:

```
[StringLength(3)]
public string ThreeLetterISOName { get; set; }
```
and in context class I've added:

```
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Language>().Property(t => t.ThreeLetterISOName).IsFixedLength();
}
```
This way it works with Sql Server Compact.

I've used EntityFramework 6.1.1 and EntityFramework.SqlServerCompact 6.1.1
Comments: Happy to help you move forward

New Post: Why is System.Web.Extensions.dll loaded when I call FirstOrDefault?

$
0
0
@AndreZiegler: We added that attribute to proxies as an attempt to fix a customer reported bug. The current behavior in which we only add the attribute (and consequently load System.Web.Extensions.dll) if System.Web.dll is already in memory was an attempt to avoid loading System.Web.dll and System.Web.Extensions unnecessarily on non ASP.NET applications and if you were not using the serializer before we create the proxies.

I agree with @ErikEJ that it would be a good idea to have Firebird also follow suite and avoid loading the assembly. I will ping the author of the Firebird provider and point him to this thread.

Reviewed: EF 6.0.0 (十一月 18, 2014)

$
0
0
Rated 5 Stars (out of 5) - 按时 123123333333333333333333333333333333333333333333333333333333333333333333333333

New Post: Changetracker - Difference between entity.OriginalValues entity.CurrentValues and entity.GetDatabaseValues

$
0
0
I found a strange Changetracker behavior.
If i load an entity from db and check the 3 values (Original,Current,Database) all seems to be ok.

currentvalue = database value (original)
originalvalue = database value (original)
databasevalue = database value (original)


But if i add a UpdateMethod in a Property Setter which updates an other property and the UpdateMethod result differ from the database value then

currentvalue = changed UpdateMethod value
originalvalue = changed UpdateMethod value
databasevalue = database value (original)

Is there something i can do that the originalvalue is the databasevalue and not the value from the dependancy update?

Created Unassigned: Add the SQL Format function to Entity Framework [2586]

$
0
0
Add the SQL 2012+ FORMAT function to System.Data.Entity.SqlServer.SqlFunctions
http://msdn.microsoft.com/en-us/library/hh213505(v=sql.110).aspx


New Post: Cannot Register a Function

$
0
0
Hi,

Is there any reason why this function cannot be registered?
[DbFunction("SqlServer", "FORMAT")]
public static string Format(this int value, string format, string culture)
{
    throw new NotSupportedException();
}
I always get a NotSupportedException: The specified method 'System.String Format(Int32, System.String, System.String)' on the type 'Model.DbContextExtensions' cannot be translated into a LINQ to Entities store expression.

Thanks,

RP

Commented Unassigned: Add the SQL Format function to Entity Framework [2586]

$
0
0
Add the SQL 2012+ FORMAT function to System.Data.Entity.SqlServer.SqlFunctions
http://msdn.microsoft.com/en-us/library/hh213505(v=sql.110).aspx

Comments: Maybe time for a general triage of which new SQL 2012 functions makes sense to implement

Edited Unassigned: Add the SQL Format function to Entity Framework [2586]

$
0
0
Add the SQL 2012+ FORMAT function to System.Data.Entity.SqlServer.SqlFunctions
http://msdn.microsoft.com/en-us/library/hh213505(v=sql.110).aspx

Edited Unassigned: Not optimal query using GroupBy and Average [2584]

$
0
0
On Entity Framework 6.1.1, given this expression:

db.GEN_PARTEOEEPERDIDAS
.Where(p => (!p.MK_BAJA.HasValue || (p.MK_BAJA.HasValue && p.MK_BAJA.Value != 1))
&& p.GEN_PARTEOEE.MK_VALIDADO.HasValue && p.GEN_PARTEOEE.MK_VALIDADO.Value == 1
&& p.GEN_PARTEOEE.FECHAPARTE.Value >= fechaInicio
&& p.GEN_PARTEOEE.FECHAPARTE < SqlFunctions.DateAdd("DAY", 1, fechaFin))
.GroupBy(g => new { ID = g.GEN_PERDIDAS.ID, Descripcion = g.GEN_PERDIDAS.DESCRIPCION })
.Select(g => new Item()
{
Categoria = g.Key.Descripcion,
Serie1Valor = g.Average(p => p.DURACION) ?? 0
})
.OrderByDescending(g => g.Serie1Valor)
.ToList();

I'm getting this SQL:

SELECT
[Project1].[C1] AS [C1],
[Project1].[DESCRIPCION] AS [DESCRIPCION],
[Project1].[C2] AS [C2]
FROM ( SELECT
[GroupBy1].[K2] AS [DESCRIPCION],
1 AS [C1],
CASE WHEN ([GroupBy1].[A1] IS NULL) THEN cast(0 as decimal(18)) ELSE [GroupBy1].[A2] END AS [C2]
FROM ( SELECT
[Filter1].[IDPERDIDA] AS [K1],
[Filter1].[DESCRIPCION] AS [K2],
AVG([Filter1].[DURACION]) AS [A1],
AVG([Filter1].[DURACION]) AS [A2]
FROM ( SELECT [Extent1].[IDPERDIDA] AS [IDPERDIDA], [Extent1].[DURACION] AS [DURACION], [Extent2].[FECHAPARTE] AS [FECHAPARTE], [Extent3].[DESCRIPCION] AS [DESCRIPCION]
FROM [dbo].[GEN_PARTEOEEPERDIDAS] AS [Extent1]
INNER JOIN [dbo].[GEN_PARTEOEE] AS [Extent2] ON [Extent1].[IDPARTEOEE] = [Extent2].[ID]
LEFT OUTER JOIN [dbo].[GEN_PERDIDAS] AS [Extent3] ON [Extent1].[IDPERDIDA] = [Extent3].[ID]
WHERE (([Extent1].[MK_BAJA] IS NULL) OR (([Extent1].[MK_BAJA] IS NOT NULL) AND ( NOT ((1 = [Extent1].[MK_BAJA]) AND ([Extent1].[MK_BAJA] IS NOT NULL))))) AND ([Extent2].[MK_VALIDADO] IS NOT NULL) AND (1 = [Extent2].[MK_VALIDADO])
) AS [Filter1]
WHERE ([Filter1].[FECHAPARTE] >= '01/04/2014 0:00:00') AND ([Filter1].[FECHAPARTE] < (DATEADD(DAY, cast(1 as float(53)), '18/11/2014 0:00:00')))
GROUP BY [Filter1].[IDPERDIDA], [Filter1].[DESCRIPCION]
) AS [GroupBy1]
) AS [Project1]
ORDER BY [Project1].[C2] DESC

The strange components of the query are:
- Double average calculation
AVG([Filter1].[DURACION]) AS [A1],
AVG([Filter1].[DURACION]) AS [A2]
- Weird case instruction
CASE WHEN ([GroupBy1].[A1] IS NULL) THEN cast(0 as decimal(18)) ELSE [GroupBy1].[A2] END AS [C2]

I was expecting just an AVG expression, not twice.

Is there an explanation for this behaviour that I'm missing?

Closed Unassigned: SqlServer Compact and Code First DataAnnotations Issue [2585]

$
0
0
Hi,

I've found a following bug when you use EntityFramework.SqlServerCompact provider:

If you wants to have nchar(n) in the database and tries to use Code First DataAnnotations to describe a property - app fails either to create database or read data from existing one

```
[StringLength(3)]
[Column(TypeName = "char")]
public string ThreeLetterISOName { get; set; }
```
It works perfectly on SQL LocalDB.

As a workaround I've used the Fluent API. First of all you have to remove [Column(TypeName = "char")] from your entity property:

```
[StringLength(3)]
public string ThreeLetterISOName { get; set; }
```
and in context class I've added:

```
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Language>().Property(t => t.ThreeLetterISOName).IsFixedLength();
}
```
This way it works with Sql Server Compact.

I've used EntityFramework 6.1.1 and EntityFramework.SqlServerCompact 6.1.1
Comments: Per comments "char" is not a supported data type on compact.

Commented Feature: UpForGrabs: Allow using "Like" operator in LINQ [2388]

$
0
0
There is no way in EF to use Ms Sql "Like" operator, and I think its very important, "Patindex" and string.contains dosent not act as Like.
#I have a table with 16,000,000 records when I use Patindex in a query it takes about 40 sec and if I replace the Patindex with Like, it take just about 1 sec to execute. I cannot use contains too, I want to use use "%" and "_" in the middle of my string search pattern.
I have searched internet for hole one day, but no one knows how to use Like in EF, all experts recommended Patindex and contained function. I downloaded the source code of EF, I though there should a way to solve this simple problem and EF design should be very extensible but unfortunately when I looked at the source I see nothing can be changed all things are just hardcoded.
I found DbLikeExpression and SqlGenerator.Visit(DbLikeExpression e) methods, but I cannot create a derived class from SqlGenerator and override "ISqlFragment Visit(DbLikeExpression e)" method and Inject my new Sql Generator class to SqlProviderServices nor icannot add new DbExpression or override DbLikeExpression functionality.
I think EntityFramework.SqlServer project is an opensource project but there is no openness and extensibility in design, all class are declared as sealed or internal and there is loss usage of DI and no one can extend the functionality, at least in the way I want.
I should change the source code of the EF and use non standard version on my project.
Comments: I have submitted a pull request at https://entityframework.codeplex.com/SourceControl/network/forks/BrandonDahler/EntityFramework/contribution/7723 to hopefully resolve this issue. I followed divega's advice on implementation, but in short it allows you to do things such as the following: ``` context.Entities.Where(e => e.String.Like("Hello~_World[.!?]", '~')).Select(e => e.String).First(); ``` This will create SQL similar to: ``` SELECT TOP 1 String FROM Entities WHERE String LIKE N'Hello~_World[.!?]' ESCAPE N'~'; ``` Please note that I am not part of the EF team and therefore cannot provide any insight on if/when this pull request may be accepted/released. Brandon Dahler

Commented Feature: Full support for Transact-SQL LIKE operator [1323]

$
0
0
I wish that the Transact-SQL [LIKE operator](http://msdn.microsoft.com/en-us/library/ms179859.aspx) is fully supported by EF.
The following three ways are known to me:
```
var ordersQuery = context.Orders.Where(o => o.ShipName.StartsWith("Welling"));

...
FROM [dbo].[Orders] AS [Extent1]
WHERE [Extent1].[ShipName] LIKE N'Welling%'

var ordersQuery = context.Orders.Where(o => o.ShipName.EndsWith("Welling"));

...
FROM [dbo].[Orders] AS [Extent1]
WHERE [Extent1].[ShipName] LIKE N'%Welling'

var ordersQuery = context.Orders.Where(o => o.ShipName.Contains("Welling"));

...
FROM [dbo].[Orders] AS [Extent1]
WHERE [Extent1].[ShipName] LIKE N'%Welling%'
```

Unfortunately, the following is not the expected result:
```
var ordersQuery = context.Orders.Where(o => o.ShipName.Contains("We%ng"));

...
FROM [dbo].[Orders] AS [Extent1]
WHERE [Extent1].[ShipName] LIKE N'%We~%ng%' ESCAPE N'~'
```

The same Problem:
```
var ordersQuery = context.Orders.Where(o => o.ShipName.Contains("Well_ng"));

...
FROM [dbo].[Orders] AS [Extent1]
WHERE [Extent1].[ShipName] LIKE N'%Well~_ng%' ESCAPE N'~'

var ordersQuery = context.Orders.Where(o => o.ShipName.Contains("Well[h-k]ng"));

...
FROM [dbo].[Orders] AS [Extent1]
WHERE [Extent1].[ShipName] LIKE N'%Well~[h-k]ng%' ESCAPE N'~'
```

The [SqlMethods.Like Method](http://msdn.microsoft.com/en-us/library/system.data.linq.sqlclient.sqlmethods.like.aspx) is unsupported outside of a LINQ to SQL context.
Comments: I have submitted a pull request at https://entityframework.codeplex.com/SourceControl/network/forks/BrandonDahler/EntityFramework/contribution/7723 to hopefully resolve this issue. I followed divega's advice on implementation from Issue #2388, but in short it allows you to do things such as the following: ``` context.Entities.Where(e => e.String.Like("Hello~_World[.!?]", '~')).Select(e => e.String).First(); ``` This will create SQL similar to: ``` SELECT TOP 1 String FROM Entities WHERE String LIKE N'Hello~_World[.!?]' ESCAPE N'~'; ``` Please note that I am not part of the EF team and therefore cannot provide any insight on if/when this pull request may be accepted/released. Brandon Dahler

Commented Issue: Singularize transforms *ges into *x [2106]

$
0
0
We have a table in our database called ParticipantRanges, and Entity framework has singularized this to ParticipantRanx.

After searching for a while only one word we can think of actually has this pluralization rule, Phalanx (phalanges). I have a hard time imagining that databases are talking about roman unit formations very often, and it's much more likely it's a word like ranges, changes, percentages, charges, images, arranges, ages, advantages, wages or some of the many other words.

I feel like this singularization rule (src\EntityFramework\Infrastructure\Pluralization\EnglishPluralizationService.cs line 1086) should probably be removed, as in the rare cases that is it true, it's a lot easier to understand why it got the singularization wrong.
Comments: To echo @assurexmichael sentiment, the workaround has been provided for code first, what about Database First?

Commented Unassigned: Error using Microsoft.SqlServer.Types NuGet Package in published Azure Mobile Service [2311]

$
0
0
Reported via a Blog comment - http://blogs.msdn.com/b/adonet/archive/2013/12/09/microsoft-sqlserver-types-nuget-package-spatial-on-azure.aspx#10529611

everything is working correcty in my local compile however when i publish to azure. I am having the same issue as @Wan

Environment is Azure Mobile Service .net backend

public class WebApiApplication : System.Web.HttpApplication

{

protected void Application_Start()

{

SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));

WebApiConfig.Register();

}

}

the dll's are copy to output director : copy allways

yet after publishing I receive this message in the azure log

Could not load assembly 'D:\home\site\wwwroot\bin\SqlServerTypes\x86\SqlServerSpatial110.dll'. Error received: 'Could not load file or assembly 'file:///D:\home\site\wwwroot\bin\SqlServerTypes\x86\SqlServerSpatial110.dll' or one of its dependencies. The module was expected to contain an assembly manifest.'.

Could not load assembly 'D:\home\site\wwwroot\bin\SqlServerTypes\x86\msvcr100.dll'. Error received: 'Could not load file or assembly 'file:///D:\home\site\wwwroot\bin\SqlServerTypes\x86\msvcr100.dll' or one of its dependencies. The module was expected to contain an assembly manifest.'.

Could not load assembly 'D:\home\site\wwwroot\bin\SqlServerTypes\x64\SqlServerSpatial110.dll'. Error received: 'Could not load file or assembly 'file:///D:\home\site\wwwroot\bin\SqlServerTypes\x64\SqlServerSpatial110.dll' or one of its dependencies. The module was expected to contain an assembly manifest.'.

Could not load assembly 'D:\home\site\wwwroot\bin\SqlServerTypes\x64\msvcr100.dll'. Error received: 'Could not load file or assembly 'file:///D:\home\site\wwwroot\bin\SqlServerTypes\x64\msvcr100.dll' or one of its dependencies. The module was expected to contain an assembly manifest.'.

Comments: Has this been fixed? I'm getting the same error..

New Post: CF: [Required] / [MinLength] data annotations insufficient

$
0
0
To my knowledge, the RequiredAttribute data annotation for string types only results in the corresponding database table column being created as NOT NULL.

I believe this is not sufficient. Moreover, it is in conflict with user interface layers, like MVC, which cannot reliably discern between an input being null or an empty string.

So I'd like to suggest to amend the behaviour of the RequiredAttribute so that it will additionally create the following T-SQL CHECK constraint: CHECK (LEN(LTRIM(RTRIM( {columnName} ))) > 0).

So an entity definition like the following:
public class MyEntity
{
    public int Id;

    [Required]
    [Index(“UK_MyEntity_Name”, IsUnique= true)]
    public string Name;
}
should result in the following DDL:
CREATE TABLE MyEntities
    ( Id INT CONSTRAINT PK_MyEntities PRIMARY KEY IDENTITY
    , Name NVARCHAR(MAX) NOT NULL CONSTRAINT UK_MyEntity_Name UNIQUE CONSTRAINT CK_MyEntity_Name CHECK (LEN(LTRIM(RTRIM(Name))) > 0)
    )
.

Similar for the MinLengthAttribute data annotation. It should, too, add the following CHECK constraint: CHECK(LEN( {columnName} ) > x ).

So an entity definition like the following:
public class MyEntity
{
    public int Id;

    [MinLength(15)]
    [Index(“UK_MyEntity_Name”, IsUnique= true)]
    public string Name;
}
should result in the following DDL:
CREATE TABLE MyEntities
    ( Id INT CONSTRAINT PK_MyEntities PRIMARY KEY IDENTITY
    , Name NVARCHAR(MAX) CONSTRAINT UK_MyEntity_Name UNIQUE CONSTRAINT CK_MyEntity_Name CHECK (LEN(Name) > 15)
    )
.

Applying both attributes in conjunction should merge the generated CHECK constraint into one:
public class MyEntity
{
    public int Id;

    [Required]
    [MinLength(15)]
    [Index(“UK_MyEntity_Name”, IsUnique= true)]
    public string Name;
}
should result in the following DDL:
CREATE TABLE MyEntities
    ( Id INT CONSTRAINT PK_MyEntities PRIMARY KEY IDENTITY
    , Name NVARCHAR(MAX) NOT NULL CONSTRAINT UK_MyEntity_Name UNIQUE CONSTRAINT CK_MyEntity_Name CHECK (LEN(LTRIM(RTRIM(Name))) > 15)
    )
.

I'm not sure whether the LTRIM/RTRIM should by mandatory in above cases or optionally added by providing an additional constructor parameter to above data annotations.

... your thoughts?

New Post: CF: [Required] / [MinLength] data annotations insufficient

$
0
0
Would a combination of MinLength and Required not fullfill your requirement?

New Post: CF: [Required] / [MinLength] data annotations insufficient

$
0
0
Yes, sure, you are right.

However, the book "Programmign Entity Framework - Code First" states that MinLength() currently isn't considered for database table column creation at all.
And I'm not sure whether it shouldn't be the default case of having empty strings being not what's intended when applying the RequiredAttribute to them. So probably (at least that's what I think) the RequiredAttribute should imply MinLength(1).
Viewing all 10318 articles
Browse latest View live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>