Error in webconfig file - asp.net-mvc

I am new to MVC 4.I am working on a tutorial which i found on browsing the internet and I am trying to add a model to my application.I have created one and when i try to add a controller it gives me a error message like..
Unable to retrive metadata for 'Practice.Models.Customer'.Invalid value for Key 'attachdbfilename'.
MODEL:
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Amount { get; set;}
}
public class CustomerDBContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
Connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-practice-20130320183458;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDBContext"
connectionString="Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\Customers.mdf;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
I think there is some problem with ConnectionString but dont know where is it.Please help me solve this problem..Thanks in advance

I think you have to make your enity virtual.
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual int Amount { get; set;}

Related

What is wrong with my Entity Framework and architecture?

I am developing a task tracker web app and you could see the full code here: https://github.com/KimSergey94/TaskTracker
In short, admin, manager, client and employee are users of the app and admin is the boss of these roles. To illustrate, a manager can create a task received by a client and assign it to an employee. The task includes Statuses and Statuses includes Comments. The "roles" have user id as a foreign key to the User table that stores their email addresses and passwords. The Role table stores user id so that they have their roles right.
I need to develop basic functionality, make use some of AJAX, custom filters, stored procedures. There is something wrong with my authorization and roles logic. So, I would appreciate if you take a look and inform me about anything that you feel is not right.
Currently, when I am trying to launch the app and initialise the database, I get the following error:
System.Data.SqlClient.SqlException: 'Introducing FOREIGN KEY constraint 'FK_dbo.Employees_dbo.Users_UserId' on table 'Employees' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
User and Employee classes code:
public class User
{
public int UserId { get; set; }
[DataType(DataType.EmailAddress)]
[Required]
public string Email { get; set; }
public string Password { get; set; }
public int RoleId { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
//[Required(ErrorMessage = "First Name is required")]
public string FirstName { get; set; }
//[Required(ErrorMessage = "Last Name is required")]
public string LastName { get; set; }
//[Required(ErrorMessage = "Country field is required")]
public string Country { get; set; }
public string Position { get; set; }
public int Salary { get; set; }
public int UserId { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
//public virtual ICollection<Task> Tasks { get; set; }
}
Globals.asax
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer<TaskTrackerContext>(new TaskTrackerDbInitializer());
var db = new TaskTrackerContext("TaskTrackerContext");
db.Database.Initialize(true);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ModelValidatorProviders.Providers.Clear();
NinjectModule orderModule = new OrderModule();
NinjectModule serviceModule = new ServiceModule("TaskTrackerDb");
var kernel = new StandardKernel(orderModule, serviceModule);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
web.config:
<connectionStrings>
<add name="TaskTrackerContext"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\TaskTrackerContext.mdf';MultipleActiveResultSets=True; Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<!--
Initial Catalog=FormsAuth;
</appSettings>
By the way why do I need this Initial Catalog=FormsAuth;?
I noticed that with this parameter I could not instantiate database.
I want to get Include functionality for my models
In the EF Core, You should disable cascade delete with DeleteBehavior.Restrict or DeleteBehavior.SetNull, e.g. in the database context class, enter a new method
protected override void OnModelCreating(ModelBuilder modelBuilder){
modelBuilder.HasOne(x => x.Employee).WithMany().HasForeignKey(x =>
x.UserId).OnDelete(DeleteBehavior.Restrict)
}
If you want cascade behavior, you need to add a nullable integer to the UserId:
public int? UserId { get; set; }
Initial Catalog=FormsAuth; is from the System.Web.Security namespace and is used for form validations, you can read more about it on the Microsoft Docs: https://learn.microsoft.com/en-us/dotnet/api/system.web.security.formsauthentication?view=netframework-4.8
I recommend using asp.net identity, I can see a lot of what you are doing is re-inventing the wheel, whereas asp.net does the authentication and validation for you. You can read about it here: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-3.0&tabs=visual-studio
I had a table that had a circular relationship with others and I was getting the same error. Turns out it is about the foreign key which was not nullable. If the key is not nullable related object must be deleted and circular relations doesn't allow that. So use a nullable foreign key.
public int? UserId { get; }
[ForeignKey("UserId")]
public virtual User User { get; set; }

Keyword not supported error in Enabling migrations

I have a dbcontext class where i have initialized 4 dbsets. My connection string is
<connectionStrings>
<add name="somename" connectionString="Data Source=.; initial catalog=someDb; user ID=ab; Password:111111; MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
My dbcotext class is
public AstroEntities(): base("somename")
{
Database.SetInitializer<AstroEntities>(new CreateDatabaseIfNotExists<AstroEntities>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Contact>().ToTable("Contacts");
modelBuilder.Entity<Appointment>().ToTable("Appointments");
modelBuilder.Entity<Consultation>().ToTable("Consultations");
modelBuilder.Entity<HomePageMessage>().ToTable("HomePageMessages");
base.OnModelCreating(modelBuilder);
}
public DbSet<Contact> Contacts { get; set; }
public DbSet<Appointment> Appointments { get; set; }
public DbSet<Consultation> Consultations { get; set; }
public DbSet<HomePageMessage> Homepagemessages { get; set; }
}
When i enable automatic migrations iam getting error as follows
"Keyword not supported: 'password:111111; multipleactiveresultsets'."
Can someone say whats the problem?
Your Connection String format is wrong, it should be like this
connectionString="Data Source=.; initial catalog=someDb; user ID=ab; Password=111111; MultipleActiveResultSets=True;"

.Net MVC Repository Pattern with EF throwing Error (Local threw an exception of type 'System.InvalidOperationException'

I am getting below error from objContext.warrantys property of entity framework.
Error Message:A null was returned after calling the 'get_ProviderFactory' method on a store provider instance of type 'System.Data.OleDb.OleDbConnection'. The store provider might not be functioning correctly.
Local 'objContext.warrantys.Local' threw an exception of type 'System.InvalidOperationException' System.Collections.ObjectModel.ObservableCollection<DSGWarrantyServiceRep.Core.warranty> {System.InvalidOperationException}
In Web.config,
public class warranty
{
public int WarrantyId
{
get;
set;
}
[Required(ErrorMessage =" Please provide Warrranty Name")]
[MinLength(4)]
public string WarrantyName
{
get;
set;
}
[Required(ErrorMessage = " Please provide Warrranty Period")]
public string WarrantyPeriod
{
get;
set;
}
[Required(ErrorMessage = " Please provide UPC Nbr")]
public string UpcNbr
{
get;
set;
}
public warranty()
{
}
}
Now, I am going to add dbcontext class.
public class WarrantyContext:DbContext
{
public WarrantyContext() : base("name=warrantyConnectionString")
{
}
public DbSet<warranty> warrantys { get; set; }
}
Now From My warranty service class implemented here
public class WarrantyService : IWarrantyService
{
WarrantyContext objContext = new WarrantyContext();
public void AddWarranty(warranty ws)
{
objContext.warrantys.Add(ws);
objContext.SaveChanges();
}
public IEnumerable<warranty> GetAllWarranty()
{
**return objContext.warrantys;**
}
}
It looks as if you need to check your connection string in the web.config (web app) or app.config (other). Feel free to post it here too. You seem to be using the OLEDB, and you should be using native SqlClient instead.
You should have something that looks like this (local db in this case, but could be SQL for you), for example:
<connectionStrings>
<add name="warrantyConnectionString" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-TestApp1-20170112094217.mdf;Initial Catalog=aspnet-TestApp1-20170112094217;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

use multiple dbContext in one application

I am struggling with using multiple dbContext with an single web application in ASP.NET MVC 5. I am following code First existing database design approach.
i need guideline how to do that say in example if i am creating 5 models using ADO.NET, it will create 5 dbContext along with its model classes.
how it will change in web.config file?
Many Thanks
public partial class DefaultContext : DbContext
{
public DefaultContext()
: base("name=DefaultContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
public virtual DbSet<sys_Actions> sys_Actions { get; set; }
public virtual DbSet<sys_ActionsInRole> sys_ActionsInRole { get; set; }
public virtual DbSet<sys_Controllers> sys_Controllers { get; set; }
public virtual DbSet<sys_Functions> sys_Functions { get; set; }
public virtual DbSet<sys_FunctionsHierarchy> sys_FunctionsHierarchy { get; set; }
}
basically for each dbContext you need to add a new connection string with unique name in the connectionStrings section in your web.config file
here is an example:
<connectionStrings>
<add name="dbContext1" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=dbServer;initial catalog=db1;integrated security = true;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="dbContext2" connectionString="metadata=res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl;provider=System.Data.SqlClient;provider connection string="data source=dbServer;initial catalog=db1;integrated security = true;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>

MVC 3 - DB Context - Web.Config

How can I share my DBcontext in my web.config without creating multiple connections?
WEB.config:
<configuration>
<connectionStrings>
<add name="daC_Companies" connectionString="Data Source=10.0.2.100;Initial Catalog=XXXXX;User ID=XXXXXXXXXx;Password=XXXXXXXX;Persist Security Info=False" providerName="System.Data.SqlClient" />
</connectionStrings>
Data Access:
public class daC_Companies : DbContext
{
public DbSet<ClassLibrary.Companies.C_Companies> dbsetC_Companies { get; set; }
}
And then I have a class called C_Companies referenced above. It works fine but I don't want to have a new DBContext for every class I want to access.
The DbContext should be specific to the database, not to the object. To add a reference to more db tables, add them as properties into the daC_Companies object:
public class daC_Companies : DbContext
{
public DbSet<ClassLibrary.Companies.C_Companies> dbsetC_Companies { get; set; }
public DbSet<ClassLibrary.Companies.Object2> Object2s { get; set; }
public DbSet<ClassLibrary.Companies.Object3> Object3s { get; set; }
public DbSet<ClassLibrary.Companies.Object4> Object4s { get; set; }
}

Resources