-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DbFunction namespace in base DbContext #13
Comments
I think it should be possible. The first argument is the namespaceName which is the name of the entity container. If you don't know what name to use try using the hint from my post: methods must have the
|
The System.InvalidOperationException: The operation cannot be performed because the item is read only. |
You need to read it to know what to put as the namespace name in the attribute, not set it. (Once the model is built it is sealed hence the exception.) |
Well, that doesn't help, because the subclass will normally have a different name to the base class. The point of having a base class is to share code, but it only works if we don't have to hardcode the namespaceName. |
Oh snap, you are right. Can you describe your scenario a bit more? Why do you have multiple derived contexts and how you use it? Do all of these context connect to the same database? Are you using the results in queries? Can the store function be converted to a query? |
I have a product which has a common set of tables, base classes and functionality. There are (currently) two other products which use the common core, and add their own tables with foreign keys to the base tables. Previously, I was using Linq to SQL, but of course the dbml files are automatically generated; it's impossible to share data access code between the projects. So each product had its own dbml, which was a maintenance problem whenever any of the base tables changed. Think of a Ideally, I want the common core to be responsible for its own data access, but if the common core has its own dbml and the other products have their own dbmls, they are different classes, and different instances at runtime, so it was very difficult to not repeat yourself. I am migrating to Entity Framework Code First largely so I can define the common tables, stored procedures and functions in a base |
Thanks for sharing your scenario. Unfortunately, I am not sure whether exposing store functions from the base DbContext class can work. @divega - do you have any ideas? |
@michaelcsikos @moozzyk A couple of thoughts:
|
Hi. Did anyone ever get around to solving/bypassing this? I have a similar problem where I used derived classes based on a root DbContext. If I create an instance of the DbContext directly all works well, but if I create an instance of the derived class it stops working. I have tried redefining the functions on the derived class etc, replacing the namespace name for that of the derived class but it doesn't work either. |
@mondayuk - no, I have not looked into this. |
I still use the same class name, |
It looks like it's not possible to declare a function in a base
DbContext
because thenamespaceName
passed to[DbFunction("DbContextClassName", "FunctionName"]
needs to be the name of the subclass.If the name of the subclass is identical but in a different C# namespace, it works, but that's not always practical. If the names are different, and we pass in the
namespaceName
of the baseDbContext
it throws the following exception when using the function:The specified method 'System.Linq.IQueryable`1[System.String] FunctionName(System.String)' on the type 'Example.BaseDbContextName' cannot be translated into a LINQ to Entities store expression.
Is this something that could be fixed, or is there another way? Thanks for your help.
The text was updated successfully, but these errors were encountered: