breeze saveChanges throws "Assembly could not be found" exception - breeze

I'm new to breezejs.
Someone please answer to this
question
I have the exact same problem on ASP.NET WepAPI 2 and EntityFramework 6.3
this is my code:
[BreezeController]
public class BreezeController : ApiController
{
readonly EFContextProvider<TodoDBEntities> _contextProvider = new EFContextProvider<TodoDBEntities>();
[HttpGet]
public string Metadata()
{
return _contextProvider.Metadata();
}
[HttpGet]
public IQueryable<Todo> Todos()
{
return _contextProvider.Context.Todos;
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _contextProvider.SaveChanges(saveBundle);
}
}
it's a simple todo app with database first entity framework and default generate code.
I think I missed something in project setup or references.
Thank you very much.

Related

EF Core + MVC 6 + .NET Core RC2 - EF not returning results

Entity Framework Core is not returning any results. I've been searching near and far. I find some tutorials saying one thing and others saying another. Here is what I have so far:
Buyer.cs
[Table("DealerCustomer", Schema = "dbo")]
public class Buyer
{
[Key]
public int DealerCustomerKey { get; set; }
public int DealerId { get; set; }
}
BuyerContext.cs
public class BuyerContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("db connection string here");
}
public DbSet<Buyer> Buyers { get; set; }
}
Startup.cs > ConfigureServices function
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<BuyerContext>(options =>
options.UseSqlServer("db connection string here");
services.AddMvc();
}
Now I am trying to load the Buyers data from my BuyerController.cs:
[Route("api/[controller]")]
public class BuyersController : Controller
{
private BuyerContext _context;
public BuyersController(BuyerContext context)
{
_context = context;
}
[HttpGet]
public IEnumerable<Buyer> Get()
{
System.Diagnostics.Debug.WriteLine("getting buyers");
System.Diagnostics.Debug.WriteLine(_context.Buyers);
return _context.Buyers;
}
}
This is all returning empty brackets when I load the page, instead of a list of Buyers. However there are over 1000 rows in that table (dbo.DealerCustomer). I know I have two places adding the db connection string but tutorials kept show both ways of doing it and when I only did it in startup.cs I was getting errors about the _context. I can make everything look pretty later, right now I just want a good connection so I have a starting place to work from.
I found there was a timeout because one of the decimal fields was returning null.
EF Core Timing out on null response

Breeze: EFContextProvider/Breeze Controller and Service Layer

When using Breeze, I was wondering how one would go about integrating it with a service layer which handles things such as email notifications, audit logs, business validations (ie Customer must exist) etc..
For example, given the following scenario:
public class SalesAppRepository
{
private readonly EFContextProvider<SalesAppContext> _contextProvider;
public SalesAppRepository(EFContextProvider<SalesAppContext> contextProvider)
{
_contextProvider = contextProvider;
}
private SalesAppContext Context { get { return _contextProvider.Context; } }
public string MetaData
{
get { return _contextProvider.Metadata(); }
}
public SaveResult SaveChanges(JObject saveBundle)
{
return _contextProvider.SaveChanges(saveBundle);
}
public IQueryable<Customer> Customers
{
get { return Context.Customers; }
}
public IQueryable<Order> Orders
{
get { return Context.Orders; }
}
/* Other entities */
}
[BreezeController]
public class BreezeController : ApiController
{
private readonly SalesAppRepository _repository;
public BreezeController(SalesAppRepository repository)
{
_repository = repository;
}
[HttpGet]
public string Metadata()
{
return _repository.MetaData;
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _repository.SaveChanges(saveBundle);
}
[HttpGet]
public IQueryable<Customer> Customers()
{
return _repository.Customers;
}
[HttpGet]
public IQueryable<Order> Orders()
{
return _repository.Orders;
}
/* Other entities */
}
I have some other business logic when entities are being created, modified etc.. such as when an Order is created; the Customer must be able to place an order (bool value canOrder) and must exist, also an email must be sent and an audit log must be updated to show the newly created order. This is just one scenario, there are other situations which would usually sit in my service layer, but I'm not sure how I would incorporate this with breeze?
I understand I can override the SaveChanges method on the SalesAppContext to generate the audit log, but what about the other situations? I'm not sure where I would put this in regards to above?
Edit
I seem to have found an, albeit, hacky way to get around this problem, I have uploaded a gist which I hope could help someone or be improved upon: https://gist.github.com/CallumVass/8300400
i've incorporated my business logic in the beforeSaveEntities(..) Method:
http://www.breezejs.com/documentation/efcontextprovider

got issue with using multiple dbContext in ASP.NET MVC 5

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.
so i have created dashboardModel using ADO.NET Entity model, that comes with its own dbContext (DashboardContext) and then roleModel using again ADO.net Entity Model (dbContext = RoleContext).
I want to keep similar concern of model separtate and their individual DBContext.
On creating DashboardModel, code run without problem but when i have created RoleModel and run; it gives me error on Dashboard controller ==> MetadataException was unhandled by user code
public DashboardContext()
: base("name=DashboardContext")
{
}
////
public class DashboardController : Controller
{
//
// GET: /Dashboard/
public ActionResult Home()
{
using (var db = new DashboardContext())
{
var query = from b in db.sys_Functions
orderby b.Function_ID
select b;
foreach(var item in query)
{
var a1 = item.Title;
}
}
return View();
}
}
//
public RoleContext()
: base("name=RoleContext")
{
}
//
public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
using(var db = new RoleContext())
{
var query = from x in db.AspNetRoles
orderby x.Name
select x;
foreach(var item in query)
{
var t = item.Name;
}
}
return View();
}
}
Many Thanks
I want to keep similar concern of model separtate and their individual DBContext.
DbContext is the abstraction for a database. So unless you are connecting your entities to different databases, there's no reason to use different Db contexts.
Note: this may not be so related to the question
I wanted to make two ApplicationDbContext with two different connections in ASP.NET MVC 5 (not ASP.NET CORE)
This is what worked for me, first you will need to add a second / overloaded constructor to ApplicationDbContext
Then add another ApplicationDbContext class like "ReadOnly_1_Local_ApplicationDbContext" that inherits the orignial ApplicationDbContext not the "IdentityDbContext" and make its constructor calls the overloaded base constructor with your new connection
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("<your connection>", throwIfV1Schema: false)
{
}
public ApplicationDbContext(string nameOrConnectionString) : base(nameOrConnectionString, throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
public class ReadOnly_1_Local_ApplicationDbContext : ApplicationDbContext
{
public ReadOnly_1_Local_ApplicationDbContext() : base("<your read only connection>")
{
}
}
Usage (I stopped sync from the first db to the read only one and changed the data in one of them to see it really connects to the other db)
public ActionResult TestMultiDbContext_1()
{
var db_context = new ApplicationDbContext();
return Content(db_context.Shipments.Find(123456).CustomerName); // name #1
}
public ActionResult TestMultiDbContext_2()
{
var db_context = new ReadOnly_1_Local_ApplicationDbContext();
return Content(db_context.Shipments.Find(123456).CustomerName); //name #2
}
That is tested with code first migrations and does not cause any issue and is not generating any extra migrations.
If you are adding another connection for entirely new db (not duplicated one), just make another class that inherits the IdentityDbContext normally
Thanks

ASP.NET MVC project filterattribute to a normal function

I have an asp.net mvc project which is divided into projectlayers:
Test has the controller which calls the taskproject. In task a function calls the database and returns the data.
TestProject:
Homecontroller()
{
public ActionResult Index()
{
List<Person> pers = PersonTask.getPersons();
return View(pers);
}
}
TaskProject:
public static List<Person> getPersons()
{
using (var context = new FilterTestEntities())
{
return context.People.OrderBy(p => p.PersonID).ToList();
}
}
I want to have a filter on the data i return to the controller. I know there is a FilterAttribute, but as far as I know this is only possible on an action function. Is it possible to put a filterAttribute on my getPersons() function. Like:
[PersonFilter]
public static List<Person> getPersons()
Thanks.

Compile error with T4MVC generated code in a MVC 3 project

We are developing a web application with ASP.Net 4 & MVC 3 Framework. I've installed T4MVC through NuGet and all the Views, Controllers and static content are succesfully generated as strong types.
But, when I try to compile the project, it raises an error at generated file T4MVC.cs, which is:
'T4MVC_ViewResultBase.FindView(System.Web.Mvc.ControllerContext)':
return type must be 'System.Web.Mvc.ViewEngineResult' to match overridden member
'System.Web.Mvc.ViewResultBase.FindView(System.Web.Mvc.ControllerContext)'
This is the source code generated:
[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
public class T4MVC_ViewResultBase : System.Web.Mvc.ViewResultBase,
IT4MVCActionResult
{
public T4MVC_ViewResultBase(string area, string controller, string action):
base() {
this.InitMVCT4Result(area, controller, action);
}
protected override void FindView(System.Web.Mvc.ControllerContext context){}
public string Controller { get; set; }
public string Action { get; set; }
public RouteValueDictionary RouteValueDictionary { get; set; }
}
The error says that:
protected override void FindView(System.Web.Mvc.ControllerContext context) { }
should be:
protected override ViewEngineResult
FindView(System.Web.Mvc.ControllerContext context) { }
But then it raises another compiling error, as this method should return code.
If we check the base class it inherits from, System.Web.Mvc.ViewResultBase, it actually declares FindView() with ViewEngineResult return type:
public abstract class ViewResultBase : ActionResult
{
...
protected abstract ViewEngineResult FindView(ControllerContext context);
}
Has anyone got this error? Has it something to do with MVC version, are we are using MVC 3?
Thanks a lot!
Sergi
I think I see the problem, and it is a T4MVC bug. But hopefully it's easy to work around.
Do you have a controller action that is declared to return a ViewResultBase? If so, can you change the return type to be ActionResult? Or alternatively you can change the return type to be whatever the concrete type is that you're returning (e.g. is it ViewResult)?
The T4MVC bug is that it doesn't correctly override non-void methods in ActionResult types.

Resources