简体   繁体   中英

Type Foo not in referenced assembly, but I'm using type Bar that extends type Foo

The following line of code produces an error:

private MyContext context;

The error:

The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced.

MyContext extends DbContext and MyContext is defined in an assembly that is referenced. I've never seen this kind of errod in Java. Why can't the compiler find a type that is defined in an assembly referenced by a dependency?

Because that assembly might not exist.

Think of it:

You have created a perfect nice DLL A which works with COM port. However, it references some kind of System.IO.Comports.dll library to work precisely as intended.

Then, someone wants to use your A library and references it. How should he know that System.IO.Comports.dll library exists in the project or bin folder or system32 directory? Or where to look for it? There is no direct reference of the EXE project that 'someone' is developing.

Direct references are always copied to the output directory, except for the preinstalled ones - like .NET Framework's - to be sure that the executable will find them.

The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced.

This type might be define in multiple libraries with multiple versions and there is no way that visual studio will look for each of them to check if it matches the required version - that's why the project needs to have the direct reference and thats why almost all .net's DLLs reference only themselves to not to bother you with additional referencing.

Because the compiler has to see the type information that is stored within its assembly, if he can't locate the assembly this error is thrown.

If the assembly is referenced it will be copied to the output directory and the compiler will be able to locate it. Also, if the needed assembly is in the GAC the compiler will locate it.

Sometimes the ResolveAssemblyReference MSBuild task which runs before the C# compiler will also locate the needed assembly and you won't have to reference it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM