LINQ to SQL, 'Linq' does not exist in the namespace 'System.Data' - c#

I tried to connect my project to a database, using LINQ to SQL, following a few online guides. I created a simple database Library, added new LINQ to SQL Classes item in the project, named SimpleLibraryDatabase, connected to the server and dragged the required tables. So far no problems, but when i tried to build the solution, it failed. Source of the problem appears to be in the auto-generated file SimpleLibraryDatabase.designer.cs, where it gives me over 70 errors, most of them saying:
The type or namespace name 'Linq' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)
What is also weird for me, in this generated context class, there is no constructor, that takes 0 arguments, unlike in all the tutorials I've watched.
This is my first time playing with databases in C#.

LINQ to SQL is a component of .NET Framework version 3.5, It's just available on .NET Framework 3.5 ~ 4.8 and not accessible to classes in the .Net core.
LINQ to SQL is a component of .NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects.
Microsoft

Related

'System.Data.Metadata.Edm.EdmFunction' is not an attribute class

I am maintaining a visual studio 2008 application (website project). There is no chance to upgrade to higher version of.net framework or upper version of entity framework. Like many IT shops, unless there are major issues, people are not going to allow me to do major upgrade on the system.
The .net framework is 3.5. and the EF version is 1.0
I need to change my program, so my select linq statement will work
Calling a SQL User-defined function in a LINQ query
As you can tell, I need to include stored function as a part of select statement
So I copied the statement.
I have struggled for hours, and I keep getting compilation.
//..various using statement
using System.Data.Objects.DataClasses;
using System.Data.Metadata.Edm;
//..other class
public static class EntityFunctions
{
[EdmFunction("FBLModel.Store", "SampleFunction")]
public static int SampleFunction(int param)
{
throw new NotSupportedException("Direct calls are not supported.");
}
}
I keep getting compilation errors
error CS0246: The type or namespace name 'EdmFunctionAttribute' could not be found (are you missing a using directive or an assembly reference?)
I have searched the whole internet include stackoverflow and MSDN blog, the namespace looks correct
Any suggestions? Thank you
The problem here is that you have a conflicting reference with your namespaces.
The System.Data.Metadata.Edm namespace contains a class called EdmFunction. The System.Data.Objects.DataClasses namespace contains a class called EdmFunctionattribute (which is the one you are trying to use).
For some reason your code is referencing the EdmFunction class. Try removing the namespace import for System.Data.Metadata.Edm, as you probably didn't want to import this in the first place.

Entity Framework - Multiple CLR Types

I am getting the following error when executing one one of my LINQ queries
The mapping of CLR type to EDM type is ambiguous because multiple CLR
types match the EDM type 'Product'. Previously found CLR type
'TF.MyProject.DAL.Product, newly found CLR type
'TF.MyProject.DTO.Product'.
The Entity Framework generated class and the DTO object both are under their own namespace. This has worked previously and below is what I have tried so far...
Regenerated the EDMX Model
Removed and Created a new EDMX Model
Run Custom Tool to generate the Template files
Ensure all projects within the solution are using the same Entity Framework version.
Entity Framework version: 6.1.3
Below is how my current project is set up
MyProject
/DAL/MyProjectModel.edmx
/DTO/Product.cs
Any idea or suggestions will be helpful. This has worked in my previous projects with version 6. Not sure if it is specific to a version within release 6..
Note: Moving the DAL to a project of its own resolves the issue
You have two classes with the same name Product. Entity Framework uses Class Names only, irrespective of namespaces or files where it is declared. The error message clearly says you have two Product classes, one in TF.MyProject.DAL namespace and another one in TF.MyProject.DTO.
Try renaming on of these Product classes to some other name and try again.

My entity framework 6 class library works from one project but not from the other

I have created a class library (targeted to .NET 4) containing all my entity framework models/context. I am referencing that class library from one project (targeted to .NET 4.5.1) and it works great, then I am referencing the same class library from another project (targeted to .NET 4) and it gives me this error:
Schema specified is not valid. Errors:
Multiple types with the name 'places' exist in the EdmItemCollection in different namespaces. Convention based mapping requires unique names without regard to namespace in the EdmItemCollection.
And then many more of the same error for all my entities.
You seem to be hitting a type conflict. When resolving types EF does not use namespaces and if it sees two types with the same names it sometimes does not know which one to use. You can find more details in the bug EF team is tracking this issue: https://entityframework.codeplex.com/workitem/483. Also note that as of EF6 most scenarios are fixed when using Code First - see this work item for more details: https://entityframework.codeplex.com/workitem/911

Build errors stemming from Entity change

Okay so I just found out I need to downgrade my solution to .NET 4.0. To do this, I upgraded to Entity Framework 6.0 (which is supposed to be .NET 4.0 compatible) and converted all my projects to target the 4.0 framework. When trying to build my data project, the EDMX has several errors. I am unable to open it normally but I can modify the XML data. Here is the error:
The type or namespace name "TABLENAME" could not be found.
I get a bunch of these for several (but not all) tables in my SQL database. Normally, I would do a database update but that does not appear possible here since I can't open the file normally. Additionally, enumerated types that have been defined in the edmx are throwing this error:
The element 'Schema' in namespace 'http://schemas.microsoft.com/ado/2008/09/edm' has invalid child element 'EnumType' in namespace 'http://schemas.microsoft.com/ado/2008/09/edm'. List of possible elements expected: 'Using, Association, CopmlexType, EntityType, Function, EntityContainer' in namespace 'http://schemas.microsoft.com/ado/2008/09/edm' as well as any element in namespace '##other'.
The XML generating this issue:
<EnumType Name="CustomEnum" />
<Member Name="Enum1">
<Member Name="Enum2" />
...
<Member Name="Enum10" />
</EnumType>
I was not the creator of this EDMX file but these enums are essential to the project.
The difference between Entity Framework 4.0 and prior and EF 4.1 and up is huge. The system was moved from 'objectcontext' to 'dbcontext'. If you are just looking to make your solution run and not upgrade to the latest I would recommend deleting all of the .tt files, turning the generation back on for the edmx and just using base EF 4.0.
If you however want to move to the new 6.0 dbcontext paradigm, you may be in for a lot of work redesigning several classes and how they interact with EF.

Oracle reference for C#

I am trying to connect to Oracle via C# but I am missing a reference to make this work:
using Oracle.DataAccess.Client;
The error is of course:
The type or namespace name 'Oracle' could not be found (are you
missing a using directive or an assembly reference?)
Since this is the standard error for missing a reference.
I am using Oracle 11.2.0.
I tried to find the reference online but I can't manage to find a working one. Also is there anything more I need in C# to connect to an Oracle database?
Where do I find the right reference?
Make sure you have installed the Oracle .Net stuff: ODP.NET.
This will include some .NET wrapper classes to the underlying Oracle client. If memory serves, they should be placed in GAC so should be easy to find from the VS.NET add references window.

Categories

Resources