I have created the following controller i want to add view that should have the data access class UserMagnament.Controller.menuitems but when i add view by right clicking on view folder,it is not showing "UserMagnament.Controller.menuitems" in add data class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UserManagement.Models;
namespace UserManagement.Controllers
{
public class menuitems
{
EvoLetDataContext db1 = new EvoLetDataContext();
public menuitems()
{ }
public IQueryable<UMUserType> menuitems()
{
return db1.UMUserTypes;
//this.Packages = _SysPackage;
//this.Modules = _SysModule;
}
public List<SysPackage> Packages { get; private set; }
public List<SysModule> Modules { get; private set; }
}
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View(new menuitems());
}
}
}
Did you rebuild your mvc application? Sometimes it helps to populate the class list when adding a View.
Related
I am trying to enable migrations for data entity: In Package Manager Console:
Enable-Migrations -ProjectName Vidly -ContextTypeName Vidly.Models.MyDBContext
I get:
Code First Migrations enabled for project Vidly.
Then
add-migration 'InitialModel'
I get: "The project 'Vidly' failed to build."
My Configuration.cs:
namespace Vidly.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<Vidly.Models.MyDBContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(Vidly.Models.MyDBContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
}
}
}
And my MyDBContext.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Vidly.Models
{
public class MyDBContext
{
public DbSet<Customer> Customers { get; set; } // My domain models
public DbSet<Movie> Movies { get; set; }// My domain models
}
}
The error message is:
error CS0311: The type 'Vidly.Models.MyDBContext' cannot be used as
type parameter 'TContext' in the generic type or method
'DbMigrationsConfiguration'. There is no implicit reference
conversion from 'Vidly.Models.MyDBContext' to
'System.Data.Entity.DbContext'.
It seems I cannot use MyDBContext as a type super class in Configuration.cs.
Please help. Thanks.
You are missing to implement DbContext.
Change AND try:
public class MyDBContext:DBContext
{
public MyDBContext() : base("ConnectionStringName")
{
}
public DbSet<Customer> Customers { get; set; } // My domain models
public DbSet<Movie> Movies { get; set; }// My domain models
}
Couple of things you are missing here.
Inheriting from DBContext
Constructor to pass the name of connection string entry you used in
use below snippet of code. note: replace VidlyDBConnectionString with your connection string name
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Vidly.Models
{
public class MyDBContext :DbContext
{
public MyDBContext():base("name=VidlyDBConnectionString")
{
}
public DbSet<Customer> Customers { get; set; } // My domain models
public DbSet<Movie> Movies { get; set; }// My domain models
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Vidly.Models;
{
public class MyDBContext :DbContext
{
public DbSet<Customer> Customers { get; set; } // My domain models
public DbSet<Movie> Movies { get; set; } // My domain models
}
}
I have the following model:
namespace Factura.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("factura.marfa")]
public partial class marfa
{
[Key]
public int idmarfa { get; set; }
[StringLength(45)]
public string denumire { get; set; }
public int idfurnizor { get; set; }
}
}
And I have also a class that inherits DbContext:
namespace Factura.Models
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
public partial class FacturaContext : DbContext
{
public FacturaContext() : base("name=FacturaContext")
{
}
public virtual DbSet<marfa> marfas { get; set; }
[...]
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
[...]
}
}
}
In my controller I have the following code:
namespace Factura.Controllers
{
[Authorize]
public class CumparatorController : Controller
{
FacturaContext dbFactura = new FacturaContext();
[HttpGet]
public ActionResult Cumparaturi()
{
var marfas = dbFactura.marfas.ToList();
return View(marfas);
}
}
}
The problem I have is that dbFactura.marfas is null, it doesn't bring anything from Database even if the table is populated.
Can anyone help me with this?
Thank you!
Remove [Table("factura.marfa")] from marfa class and add the following in your OnModelCreating in FacturaContext
modelBuilder.Entity<marfa>().ToTable("factura_marfa"); //updated . with _
Create the migration & run
// Here is the controller that reads a file uploaded.It works successfully when file is uploaded to App_Data,but when uploaded to MS SQL Server it gives error as "Invalid Object dbo.Prescription"
using SastiDawaai.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
namespace SastiDawaai.Controllers
{
public class MedicineController : Controller
{
// GET: Medicine
public ActionResult Index()
{
MedicineContext medctx = null;
using(medctx=new MedicineContext())
{
List<Medicine> medicinelist=medctx.Medicines.ToList();
return View(medicinelist);
}
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
MedicineContext medctx = null;
Prescription pres = null;
if(file!=null && file.ContentLength>0)
{
////upolading files to App_Server
var filename = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data"), filename);
file.SaveAs(path);
//uploading Files to database
var file_content=new BinaryReader(file.InputStream);
var content=file_content.ReadBytes(file.ContentLength);
using (medctx = new MedicineContext())
{
pres = new Prescription
{
Prescription_Receipt = content,
File_Name = filename,
Submitted_Date = DateTime.Now,
};
medctx.Prescriptions.Add(pres);
medctx.SaveChanges();
}
}
return RedirectToAction("Index");
}
}
}
//Here is the Model for Prescription
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace SastiDawaai.Models
{
public class Prescription
{
[Key]
public int Prescription_Id { get; set; }
public byte[] Prescription_Receipt { get; set; }
public DateTime Submitted_Date { get; set; }
public string File_Name { get; set; }
}
}
//here is the DBContext Class having 2 DBSets "Medicine" and "Prescription"
//There is no issue in getting records and inserting Records in Medicine entity,the issue only occurs when adding record to any additional DBSet added to the context.
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace SastiDawaai.Models
{
public class MedicineContext:DbContext
{
public virtual DbSet<Medicine> Medicines { get; set; }
public virtual DbSet<Prescription> Prescriptions { get; set; }
}
}
Check you Table name in your database
you entity is Prescription and your table name should be PrescriptionS,and also check you table schema, in your Entity it's Dbo so your table in your database should be the same schema.
Could somebody please explain how I retrieve a simple set of records from a SQL Server 2012 database to display on an ASP.NET page? I have tried following some tutorials but I am missing something and I cant see the wood for the trees.
I have a connection string in the web.config file:
<add name="DefaultConnection"
connectionString="Data Source=MY-LAPTOP;Initial Catalog=Test;Integrated Security=True"
providerName="System.Data.SqlClient" />
I have a class called Navigation.cs which is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public partial class Navigation
{
public int navigation_ID { get; set; }
public string title_TV { get; set; }
public Nullable<int> position_IV { get; set; }
public bool main_BT { get; set; }
public string url_TV { get; set; }
}
}
I have a Shared/_layout.cshtml page which has the following code:
#model List<WebApplication1.Models.Navigation>
#foreach (var item in Model) {
<li>#Html.DisplayFor(modelItem => item.title_TV)</li>
}
I also have a controller called HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
I get the following error
An exception of type 'System.NullReferenceException' occurred in App_Web_wqu1n0vj.dll but was not handled in user code.
I have created a class called TestEntities.cs which is as follows:
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
namespace WebApplication1.Models
{
public partial class TestEntities : DbContext
{
public TestEntities()
: base("name=DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Navigation> Navigations { get; set; }
}
}
Any help would be much appreciated :-)
I have a dbml file that has stored procedures dragged off. I have an EmployeeModel class that has get and set properties.
I have an interface IEmployee and an EmployeeRepository repository that has the implementation of the methods. Please refer to the code. In the stored procedure GetRoles I just have SELECT * FROM ROLE.
In the repository, how do I loop through the resultset? Can I change ISingleResult to IMultipleResult in the dbml designer file?
EmployeeModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcWebsite.Models
{
public class EmployeeModel
{
public int RoleId { get; set; }
public string RoleName { get; set; }
public string Description { get; set; }
public string TaskMark { get; set; }
public int RoleFlag { get; set; }
}
}
IEmployee:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcWebsite.Models;
namespace MvcWebsite.DAL
{
public interface IEmployees
{
IList<EmployeeModel> ListAll();
// void Save(EmployeeModel employ);
}
}
EmployeeRepository.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcWebsite.Models;
using System.Data.Linq;
namespace MvcWebsite.DAL
{
public class EmployeeRepository:IEmployees
{
private DataDataContext _dataContext;
public EmployeeRepository()
{
_dataContext = new DataDataContext();
}
public IList<EmployeeModel> ListAll()
{
//IMultipleResults result =_dataContext.GetRoleDetails();
//var Emps = result.GetResult(EmployeeModel);
List<EmployeeModel> emp = _dataContext.GetRoleDetails();
// foreach (GetRoleDetailsResult role in Emps)
// {
// role.Description=Emps.
// }
return Emps.ToList();
}
}
}
Error:
Error 1 Cannot implicitly convert type
'System.Collections.Generic.List'
to
'System.Collections.Generic.IList'.
An explicit conversion exists (are you missing a
cast?) C:\Users\guhananth\documents\visual studio
2010\Projects\MVC_SP\MvcWebsite\DAL\EmployeeRepository.cs 44 19 MvcWebsite
Typicall you would use:
List<EmployeeModel> emp = _dataContext.Employees.ToList();
You can read about a Entity Framework implementation of the Repository pattern here: http://blog.gauffin.org/2013/01/repository-pattern-done-right/