Reputation: 129
I have MVC first code project but I get below error
First code! So there is no default database.
Model class
public class Applicant
{
public int ApplicantID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int PhoneNumber { get; set; }
}
Context class:
public class Context : DbContext
{
public List<Applicant> Applicants { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Applicant>().Property(r => r.ApplicantID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
Applicant repasority class:
public Applicant Create(string username, string password, int phoneNumber, out int existResult)
{
if (context.Applicants.Where(x => x.UserName == username).ToString() != null)
{
applicant = new Applicant()
{
UserName = username,
Password = password,
PhoneNumber = phoneNumber,
};
context.Applicants.Add(applicant);
existResult = 1;
return applicant;
}
existResult = 0;
return applicant;
}
And I added below code to my web.config in my web solution:
<connectionStrings>
<add connectionString="Data Source=.;Catalog=Homi;Integrated Security=True" providerName="System.Data.SqlClient" name="Context" />
</connectionStrings>
</configuration>
Upvotes: 0
Views: 50
Reputation: 129
ok I find answer.
first I changed context class to this like below
add this part:
public Context()
{
base.Configuration.LazyLoadingEnabled = false;
base.Configuration.ProxyCreationEnabled = false;
base.Configuration.ValidateOnSaveEnabled = false;
}
and changed string to DbSet:
public DbSet<Applicant> Applicants { get; set; }
second
a cool mistake :D I forgot to do this:
Project –> Manage NuGet Packages…
Note: If you don’t have the Manage NuGet Packages… option you should install the latest version of NuGet
Select the Online tab
Select the EntityFramework package
Click Install
after all for some place of code if above ways doesn't work try{}catch{} was last solution!
and in end when I run my web nothing saved in my db :( it was very important to put "context.SaveChanges();" in applicant repository after "context.Add(applicant);"
that's all tnx
Upvotes: 1