Entity Framework meta data - c#

I have a piece of code
using (InContext inContext = new InContext())
When I right click the definition, it points to C:\Users\me\AppData\Local\Temp\....
What is that? A defined dll?
#region Assembly SharedObjects.dll, v4.0.30319
// C:\source\blah\blah\packages\SharedObjects.1.0.4659.22817\lib\net40\SharedObjects.dll
#endregion
using blahSharedObjects;
using System;
using System.Data.Entity;
namespace something
{
public class InContext : DbContext
{
public InContext();
public InContext(string connectionString);
public DbSet<InDetails> InDetailRecords { get; set; }
public string IPAddress { get; set; }
public string UserName { get; set; }
}
}

The location you are referring to is your local user account temp folder. Is the InContext class in your App_Code folder? Is the name of the DLL your referring to the same as your project?
I can only assume it's being built with the solution which in turn put it in the temp folder. Try cleaning/rebuilding to see if it is still the same.

Related

Problem with namespace from a nested folder

I have a problem using a namespaced component which works in some places but not others:
Pages/Transactions/List.razor
<Test #ref="Test1"></Test>
Pages/Transactions/List.razor.cs:
using Accounting.Web.Components.Test;
namespace Accounting.Web.Pages.Transactions
{
public partial class List
{
private Test Test1 { get; set; } = default!;
}
}
The above reference to Test works as expected.
But then I want to to use the Test component in another component, so I do the following:
Components/Transactions/TRansactionRules/TRansactionRules.razor.cs
using Accounting.Web.Components.Test:
namespace Accounting.Web.Components.Transactions.TransactionRules
{
public partial class TransactionRules
{
[Parameter]
public Test Test1 { get; set; } = default!; // error
}
}
But using it in the component above produces the following error:
"Test" is a namespace but used as a type
At which point I have to replace the line with:
public Accounting.Web.Components.Test.Test Test1 { get; set; } = default!;
while elsewhere I can refer to it as Accounting.Web.Components.Test
It seems the folder / file structure has something to with it:
+Pages
+Transactions
-List.razor
-List.razor.cs
+Components
+Test
-Test.razor
-Test.razor.cs
+Transactions
+TransactionRules
-TransactionRules.razor
-TransactionRules.razor.cs
It seems when I try to use the Test component in a another component which is nested in a extra subfolder, reference to it fails, otherwise it will work.
I wish to refer to it as Accounting.Web.Components.Test rather than Test.Test regardless of where it is used. Am I doing something wrong, if so what?
I belive the error you are getting is because you are 'using' a namespace which also contains the full name of he type you are trying to use. I think removing .Test from the using directive should fix the issue.
using Accounting.Web.Components
namespace Accounting.Web.Components.Transactions.TransactionRules
{
public partial class TransactionRules
{
[Parameter]
public Test Test1 { get; set; } = default!; // error
}
}
(optional) You can move below code inside of _Imports.razor file in blazor solution, and outside of your component code. This way it will make it available everywhere into your blazor solution and not just one component file.
using Accounting.Web.Components

C# EF Required Attribute not recognized

I am creating an app that uses a database (SQLite). I am using entity framework and ADO.NET to interact with it.
I have a seprate Models project in my app that contains all of my Database models.
Now I want to mark some of my class properties as required to reflect the "NOT NULL" option in my database. But if I add the [Required] Attribute from DataAnnotations namespace I get a compiler error saying it cannot be resolved.
Here is how my class looks like :
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ReflowModels
{
public class Tag
{
public Tag()
{
this.Options = new HashSet<Option>();
}
public int Id { get; set; }
[Required]
public string Name { get; set; }
public ICollection<Option> Options { get; set; }
}
}
I have also added a reference to EntityFramework.dll in my project.
you need to add this to your using block
using System.ComponentModel.DataAnnotations.Schema;
you need to add this to your using block
using System.ComponentModel.DataAnnotations;
In case it still doesn't work, maybe you should add it to your References

Enum cannot work when reference it cross multi visual studio solution

I have three solution A, B and C.
I have an enum in A like this:
using System.Runtime.Serialization;
namespace A.Entities
{
[DataContract]
public enum Status
{
[DataMember]
Active = 0,
[DataMember]
Inactive = 1,
}
}
I reference it in solution B like this:
using A.Entities;
namespace B.Entities
{
public class User
{
[DataMember]
public string UserName { get; set; }
[DataMember]
public Status Status { get; set; }
}
}
In solution C, I use them like this:
using B.Entities;
using Status = A.Entities.Status;
namespace C.TestDatas
{
public class UserTestData
{
public static User CreateUser()
{
return new User
{
Status = Status.Active,
}
};
}
}
When I invoke User method, it throw exception:
Method not found: 'Void B.Entities.User.set_Status(A.Entities.Status)'.
Why? I hope someone can help me, thanks!
I can't reproduce the problem. Your code works fine on my computer (on .NET framework 4.5)
Could you make sure that you project is configured correctly:
Both A and B solutions have reference to System.Runtime.Serialization
C has reference to A and B. Make sure you don't link the dll from /Bin folder, but you have the project reference
Hit Clean and Rebuild buttons in Visual Studio.

Cannot implicitly convert linq result into viewmodel list

I'm new to MVC and struggling to implement a ViewModel to query multiple tables. Initially my setup worked perfectly but now having reloaded the project I am getting a compilation error as copied below:
Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>'
ViewModel code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestProject.Models
{
public class COMPCATEGORY
{
public List<COMP> Comp { get; set; }
public List<CATEGORY> Category { get; set; }
}
}
Controller Code:
namespace TestProject.Controllers
{
public class COMPsController : Controller
{
private mattbeaneyEntities1 db = new mattbeaneyEntities1();
// GET: COMPs
public ActionResult Index()
{
COMPCATEGORY viewModel = new COMPCATEGORY();
viewModel.Category = db.CATEGORies.ToList();
viewModel.Comp = db.COMPs.ToList();
return View(viewModel);
}
DB Context code:
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class mattbeaneyEntities1 : DbContext
{
public mattbeaneyEntities1()
: base("name=mattbeaneyEntities1")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<CATEGORY> CATEGORies { get; set; }
public virtual DbSet<COMP> COMPs { get; set; }
}
As the error suggests it is having trouble with two types by the same name, when we were only expecting one. The namespace is our clue here:
Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>'
Usually there are a couple of things I like to check in this case:
Firstly: are there two classes that share the same name and belong to different namespaces? It's quite easy to do as your project grows!
Secondary: has the main project namespace changed? Sometimes due to a bit of refactoring we change the project name and then end up with two .dll files in the bin folder, which hold duplicate of all our classes - delete the old one!

Entity Framework DbContext code generation generates incorrect code

I'm using VS2010 with Entity Framework (file version is 4.4. product version is 5)
I have installed the EF5.x DbContext generator.
After creating my .edmx file, I right clicked on the empty space and added a new DbContext template, which generated the context.tt and .tt files.
However, in the .tt files, this is how the auto generated code looks like:
namespace DataObjects.EntityFramework.Models
{
using System;
using System.Collections.Generic;
public partial class SubSystem
{
public string SubSystemId { get; set; }
public string Description { get; set; }
public string Fmode { get; set; }
public Nullable<System.DateTime> LastBackup { get; set; }
}
}
The problem is that the using statements are inside the namespace, which gives rise to a compilation error.
Those compilation errors must be related to something else, because it's perfectly legal in C# to have using statements in the namespace.
Verify that you've added all of the correct references, such as EntityFramework.dll

Categories