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;"
Related
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>
I have the following code to create entities and seed them with data for use in an asp.net mvc application. I am using code first and entity framework. I generate controllers and run the application but on the index.cshtml page my list is empty where the seed data should be.
public class MyContext : DbContext
{
public MyContext() : base("dataDb") {}
public DbSet<Owner> Owners { get; set; }
public DbSet<Pet> Pets { get; set; }
}
public class MyInitializer : DropCreateDatabaseAlways<MyContext>
{
protected override void Seed(MyContext context)
{
// seed database here
context.Owners.AddOrUpdate(
new Owner()
{
//name=,
//id=,
Pets = new List<Pet> { new Pet()
{
//id=,
//name=,
},
new Pet()
{
//id=,
//name=,
}}
},
new Owner()
{
//id=,
//name=,
Pets = new List<Pet> { new Pet()
{
//id=,
//name=,,
}
}
}
);
context.SaveChanges();
}
}
public class Owner
{
public int OwnerId { get; set; }
public string Name { get; set; }
public virtual List<Pet> Pets { get; set; }
}
public class Pet
{
public int PetId { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public virtual Owner Owner { get; set; }
}
}
I found a solution to this problem:
Database.SetInitializer(new MyInitializer()); //important for seed to work
add this line to the context constuctor:
public MyContext() : base("dataDb") {}
You can also add it to your web.config. When it comes to deployment, you can easily remove it from the web.config by using configuration transformer.
<entityFramework>
<contexts>
<context type="MyProject.MyContext, MyProject">
<databaseInitializer type="MyProject.MyInitializer, MyProject" />
</context>
</contexts>
...
</entityFramework>
<connectionStrings>
<add name="MyContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=OwnersPets; AttachDBFilename=|DataDirectory|\OwnersPets.mdf; Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionString>
Example conn string to use with localDb
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>
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;}
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; }
}