C# Connect database to Visual Studio project - c#

I have just started studying databases and now I'm trying to connect to a SQL database with Visual Studio. I made the main model. There are tables and stuff. Now I created a console application. I made reference to the project with the data (where the diagram with tables is). But when I try to compile the project I get this error
error CS1502: The best overloaded method match for
'System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction(string,
params System.Data.Entity.Core.Objects.ObjectParameter[])' has some
invalid arguments
and this error
error CS1503: Argument 2: cannot convert from
'System.Data.Objects.ObjectParameter' to
'System.Data.Entity.Core.Objects.ObjectParameter[]'
And on the other project with the data I get this error
error CS0006: Metadata file 'C:\Users\United\Documents\Visual Studio
2012\Projects\C#\EntityFrameworkDemo.Data\EntityFrameworkDemo.Data\bin\Debug\EntityFrameworkDemo.Data.dll'
could not be found
I couldn't find a solution to any of the errors. Can you suggest something?
Here is the sample code that I'm trying to run:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFramework;
namespace EntityFramework
{
class Program
{
static void Main(string[] args)
{
NorthwindEntities db = new NorthwindEntities();
foreach (var customer in db.Customers)
{
Console.WriteLine(customer.ContactName);
}
db.Dispose();
}
}
}

It looks like you have conflicts between the types defined in the Sysytem.Data.Entity assembly and the EntityFramework assembly. If you're using EF 6 or later, remove all references to System.Data.Entity and try recompiling. You may need to remove some lingering using statements for those types as well.

Related

.NET Assembly issue with MATLAB [duplicate]

I used the MATLAB Compiler to generate a .NET Assembly with a very little MATLAB code:
function output_arg = extest( input_arg1,input_arg2 )
output_arg = input_arg1+input_arg2;
end
I generated the dll with the wizard.
Within my Visual Studio project I added the reference to the generated dll (extest.dll) and to the MATLAB Runtime dll (C:\Program Files\MATLAB\MATLAB Runtime\v92\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll) as mentioned in the "Assembly Description".
This is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathWorks.MATLAB.NET.Utility;
using extest;
namespace DllTesting
{
class Program
{
static void Main(string[] args)
{
ExClass e1 = new ExClass();
}
}
}
It builds without errors an intelisense is working (so all references should be good from my understanding)
But when I launch it, the following exception gets thrown (on new ExClass()):
An unhandled exception of type 'System.TypeInitializationException' occurred in DllTesting.exe
Additional information: The type initializer for 'extest.ExClass' threw an exception.
Any suggestions what is wrong with this code or whats missing?
Try adding this before the class definition
[assembly: MathWorks.MATLAB.NET.Utility.MWMCROption("-nojit")]
Also make sure that the .NET version you use for assembly is the same or lower than the one used for your Visual Studio project.
Another solution might be adding the path of the MATLAB runtime (e.g. C:\Program Files\MATLAB\MATLAB Runtime\v92\runtime\win64) to the PATH Environment Variable.
If none of these helps, have a look here and here, you might have a 64/32 bit mismatch.

Using AssembliesFromPath from StructureMap throws MissingMethodException

I am trying to scan all assemblies in a subfolder of my main project and then add the contained registry to my main registry in order to override default registry entries, where necessary and intercept types for dependency injection.
For this I create the following registry
public PluginRegistryAdder(string pluginPath)
{
Scan(x =>
{
x.AssembliesFromPath(pluginPath);
x.LookForRegistries();
});
}
which I plan to add to my main container like this:
var pluginRegistries = new PluginRegistryAdder(pluginPath);
Container.Configure(_ => _.IncludeRegistry(pluginRegistries));
The problem I am now facing is that I get this error, when using AssembliesFromPath and I don't know how to fix it:
System.MissingMethodException: Method not found: 'Void StructureMap.Graph.IAssemblyScanner.AssembliesFromPath(System.String)'.
Googling suggests that I should use the namespace StructureMap.Graph, but this did not solve the problem. Here are my using statements, of which only StructureMap is not greyed out (e.g. marked as not redundant) by Visual Studio (I was trying to find the missing reference, but nothing helped):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StructureMap;
using StructureMap.Graph;
using StructureMap.Configuration.DSL;
using StructureMap.Configuration;
using StructureMap.Graph.Scanning;
using StructureMap.Util;
Any suggestions, what I should try?!
UPDATES:
Note that when I comment out just the line with AssembliesFromPath, solution runs just fine, although LookForRegistries is definded in the same class as AssembliesFromPath.
AssembliesFromApplicationBaseDirectory gives me the same problem.
This problem seems to have been caused by a version mismatch between my DI project and my main project. After some trying I ended up reinstalling the newest version of StructureMap in both projects and the problem went away.

DataClassesDataContext Not Found - VS2012

I'm doing a project in ASP.NET and I'm using Visual Studio 2012 with .NET 4.5.
I've been searching online for my problem but haven't found any solution yet. Maybe anyone here can help me.
I'm working on a project and started with an empty Web Application. I've added a folder "App_Code" in which I put my Models and DataClasses. I've added a LINQ-to-SQL class with the correct tables and saved this file in the App_Code folder.
Now, when I add a Class to the App_Code folder, the class can't find the DataClasses.
This is the code of my class "Product" in the folder App_Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Outside.App_Code
{
public partial class Product
{
private static OutsideDataClassesDataContext db;
public static List<Product> GetAll(int id)
{
// Init the DB
db = new OutsideDataClassesDataContext();
// Get all the products and return
return db.Products.Where(a => a.id > 0).ToList();
}
}
}
Visual Studio won't let me run the code. I get this error:
The type or namespace name 'OutsideDataClassesDataContext' could not be found (are you missing a using directive or an assembly reference?) \App_Code\Product.cs 10 24 Outside
The build action of the file Product.cs is "Compile".
Can anyone help or give advice about this? Or try to help get the DataClasses recognised? I'm still learning ASP.NET. If you need other data or other code, just tell me.
Thanks in advance!
I've removed everything from the folder App_Code and made a new folder called Models. Placed everything (class Product and a new OutsideDataClass) in the folder Models and suddenly it works. Weird.
App_Code seems like a bad idea.
Firstly, make sure you have Linq to SQL added manually from the Visual Studio installer.
It will be under 'Individual Components'
The error is looking for the Linq dbml file that makes a generic version of the database you are connected to, to add this dbml right-click on your solution/project;
Choose Add New Item, then under Data, choose Linq-To-SQL Classes . This will create an empty DBML file. You'll then need to create a server connection to a database (if you have not already) and drag the tables you want into the DBML designer.
Looks like your OutsideDataClassesDataContext() is
in another namespace or in another project of your solution.
Try to shorten your namespace name to Outside
or add reference from project, where your class exists.

Visual Studio 2012 Web Express Compile Project with EDMX

I created a project in Visual Studio 2012 Web Express. After that I included an EDMX file and updated it with database generated models. Now when I perform a build, Visual Studio lists down alot of errors all relating to the piece of code I have written below.
The compiler points the following as errors When I open the Model class. This is the auto generated model class.
namespace Mvc
{
using System;
using System.Collections.Generic;
public partial class MyTableName
{
public MyTableName()
{
The moment I move using statement outside the namespace as follows, the errors get resolved.
using System;
using System.Collections.Generic;
namespace Mvc
{
public partial class MyTableName
{
public MyTableName()
{
I havent faced this issue before and what I performed is simply out of the box. Any clue whats going wrong?
Oops! Looks like the database had a table with a reserved C# class name. Issue Resolved.

Windows Store Project - Assembly reference error when trying to run cmd commands from a C# file in VS2012

I'm doing some exercises with C# in the trial version of VS 2012. I want to execute a cmd command from a CS file. For this, I've tried Process.Start as well as System.Diagnostics.Process that are mentioned in these posts:
Run Command Prompt Commands
Execute CMD command from code
However, despite I added "using System.Diagnostics" and "using System.ComponentModel", I'm still getting "The type or namespace name 'Process' does not exist in the namespace 'System.Diagnostics', missing assembly reference" error. ¿Any suggestion so I can i get rid of this error? Thanks in advance.
This usually happens when you have Target framework = .NET Framework Client Profile, but DLL you reference is from .NET Framework (full). Make sure you have System.dll in your references from valid framework.
I just did the same - created empty console application with the following code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var prc = Process.Start("explorer.exe");
}
}
}
Works perfectly fine for me.
Additional thing to check is Intellisense - when you start typing "System.Diagnostics.Proc"... - does it show you dropdown with "Process" there?
UPDATE:
Windows Store projects are based on different version of target .NET Framework - .NET for Windows Store apps, which does not support functionality you need.
For more details do web search:".NET for Windows Store apps". Helpful links:
http://msdn.microsoft.com/en-us/library/windows/apps/br230302.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/br230232.aspx

Categories

Resources