zzzprojects / EntityFramework-Classic

Entity Framework Classic is a supported version of the latest EF6 codebase. It supports .NET Framework and .NET Core and overcomes some EF limitations by adding tons of must-haves built-in features.
https://entityframework-classic.net
Other
103 stars 27 forks source link

HasNoKey and Constructor with Parameters #56

Closed LukeTOBrien closed 4 years ago

LukeTOBrien commented 4 years ago

Hello,

I am converting a project from Entity Framework Core to Entity Framework Classic due to some limitations of things that I used to be able to do in EF6 but no longer can... It seems there are also some things you can do in Core that you cannot do in ES6

The first one is that I have views in my database, these views are made from joining a number of tables together, the views have no primary key and with Core we can do .HasNoKey() in the DB context.
Any chance of you adding this to your framework?

The other thing is not so important, I can get around it by Select().AsEnumerable(), but it would be nice.
I am getting ye olde Only parameterless constructors and initializers are supported in LINQ to Entities because I have a class like this:
(I guess I could refactor)

public class NameValue<T>
{
  public string Name { get; set; }
  public T Value { get; set; }
  public NameValue(string name, T value)
  {
    Name = name;
    Value = value;
  }
}

What do you think?
The HasNoKey issue will mean that I am stuck untill I figure out the unique composite key combination.

JonathanMagnan commented 4 years ago

Hello @LukeTOBrien ,

HasNoKey

We will look if that's possible for the HasNoKey. Obviously, the same restriction will happen (you can select but you cannot insert, update, or delete.

Parameterless Constructors

As for the second, it looks to be harder. What if there is more than one constructor with a parameter? Which one do we take?

We need a way to be able to create a new instance of this class. For example, a factory that you tell us how to create it. We will look at it and try to propose a solution if that's possible to support this.

LukeTOBrien commented 4 years ago

Thanks!!

Don't worry to much about the parameters constructor if it's too much.

On Tue, 26 May 2020, 23:39 Jonathan Magnan, notifications@github.com wrote:

Hello @LukeTOBrien https://github.com/LukeTOBrien , HasNoKey

We will look if that's possible for the HasNoKey. Obviously, the same restriction will happen (you can select but you cannot insert, update, or delete. Parameterless Constructors

As for the second, it looks to be harder. What if there is more than one constructor with a parameter? Which one do we take?

We need a way to be able to create a new instance of this class. For example, a factory that you tell us how to create it. We will look at it and try to propose a solution if that's possible to support this.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/zzzprojects/EntityFramework-Classic/issues/56#issuecomment-634317840, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD3KMFI4MDMNMZXNENZYOWTRTRALDANCNFSM4NKO3O3Q .

JonathanMagnan commented 4 years ago

Hello @LukeTOBrien ,

We tried for the HasNoKey but more we advance on this request, more we discover that we have more code to modify which might have some side impact. The core of EF6 was not been built to support entity without a key.

However, one easy solution on your side would be:

Is that a viable solution for you?

LukeTOBrien commented 4 years ago

Thanks for looking into this for me, I appreciate it.

On my side there might not be a lot I can do with the views, there are perhap over 100 and I might not be allowed to change them. I have already added a few views onto my contexts and at the moment it seems to be okay, but I have basically just guessed at what the unique ID is.

Thank you for your assistance, I think I shall just plough on.

On Wed, Jun 3, 2020 at 4:19 PM Jonathan Magnan notifications@github.com wrote:

Hello @LukeTOBrien https://github.com/LukeTOBrien ,

We tried for the HasNoKey but more we advance on this request, more we discover that we have more code to modify which might have some side impact. The core of EF6 was not been built to support entity without a key.

However, one easy solution on your side would be:

  • Add a property ID of type Guid in your entity
  • Return in your view a new column NEWID() AS ID

Is that a viable solution for you?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/zzzprojects/EntityFramework-Classic/issues/56#issuecomment-638266269, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD3KMFO3JX2CZBGPT5CFMSDRUZSXHANCNFSM4NKO3O3Q .

JonathanMagnan commented 4 years ago

Depending on if you wish to go in some "hack" solution or not you can also use an interceptor and replace some part of the command text.

If the command text contains a SELECT and your view name, then you replace the first SELECT statement by "SELECT NEWID() AS ViewKeyID"

SELECT NEWID() AS ViewKeyID, Column1, Column2, Column3
FROM ...
WHERE ...

Might not be a good long term solution however, it really depends on your requirement.