Child class of DbContext returns no data - entity-framework-4

Here's my sub-class of my DbContext:
public class JalsoxDbContextCache : JalsoxDbContext, IJalsoxDbContext
{
}
Any ideas why it would return no data where as if I use the base class instead it does?

The name of the class is used to create the connection string.
So I changed the derived class to this:
public class JalsoxDbContextCache : JalsoxDbContext, IJalsoxDbContext
{
public JalsoxDbContextCache()
: base("JalsoxDbContext")
{
}
}
How strange!

Related

Access controller level variable from action method attribute

I have the following base class for all controllers-
public abstract class BaseController:Controller
{
public string BaseUrl
{
get { return "something"; }
}
}
I also have the following action filter attribute-
public class CheckQueryStringAttribute : ActionFilterAttribute
{
string baseUrl;
public CheckQueryStringAttribute(string BaseUrl)
{
baseUrl = BaseUrl;
}
}
I would like to use BaseUrl from base controller into attribute as follows-
public class LoginController : BaseController
{
[CheckQueryString(BaseUrl)]
public ActionResult LoginSuccess()
{
return View();
}
}
Is there any way to do it?
You couldn't pass a variable or object reference in attribute constructor parameter because attributes will resolve at compile time so you can only pass constant by their constructor.
But if you exactly explain what you want to do may i can solve your problem in other way.

Passing the dbcontext to the repository: A field initializer cannot reference the nonstatic field, method, or property

I have the following controller, in which I create an instance of the BadgeAssignmentRepository. I tried to pass the my dbcontext variable in the declaration of the repository. However I receive A field initializer cannot reference the nonstatic field, method, or property EntryController.db
I have no idea why my code is wrong. Can someone help me?
Here is the controller:
public class EntryController : Controller
{
public EchoLuMvcDbContext db = new EchoLuMvcDbContext();
private BadgeAssignmentRepository baRepository= new BadgeAssignmentRepository(db);
//this db is causing the trouble
Here is the repository:
public class BadgeAssignmentRepository
{
public EchoLuMvcDbContext db { get; set; }
public BadgeAssignmentRepository(EchoLuMvcDbContext context)
{
this.db = context;
}
As the error says, you can't access another field from a field initializer. If your BadgeAssignmentRepository needs a reference to your db field, initialize it in your controller's constructor like this:
public class EntryController : Controller
{
public EchoLuMvcDbContext db = new EchoLuMvcDbContext();
private BadgeAssignmentRepository baRepository;
public EntryController() {
baRepository = new BadgeAssignmentRepository(db);
}
}

Configure Structuremap IOC

Hello Experts I have class in folowing structure
public class Apple:IApple
{
public IBall _ball{get;private set}
public Apple()
{
_ball=new Ball();
}
}
Public class Cat:ICat
{
private readonly IBall _ball;
Public Cat(IBall ball)
{
this._ball=ball
}
}
I am configuring my structure map as below (Not displayed standard registry and controller registry with controller conventions)
ObjectFactory.Container.Configure(cfg =>
{
cfg.For<IApple>().Use<Apple>();
cfg.For<ICat>.Use<Cat>();
});
When I reference ICat in Controller as below
private readonly ICat _cat;
public HomeController(ICat cat)
{
this._cat = cat;
}
I am receiving error "No default Instance is registered and cannot be automatically determined for type IBall".
Experts, please provide some suggestions.
You should register IBall like this:
cfg.For<IBall>().Use(c => ((Apple)c.GetInstance<IApple>())._ball);

Extending the DataAnnotations attributes for MVC in asp.net

Brad Willson has a great article on descripting how to use DataAnnotations. http://bradwilson.typepad.com/blog/2009/04/dataannotations-and-aspnet-mvc.html What I would like to do is extend the available attributes that I can use. Something like [ PastDate(you must enter a date in the past)] or [InvoiceNumber( all invoices start with INV and end with 002)]. I know that I could use the Regular expression attribute to accomplish this. However having more descriptive attributes would be a cleaner solution.
You need to create a class that inherits from System.ComponentModel.DataAnnotations.ValidationAttribute and then use that attribute like this :
public class yourModel {
[CustomValidation(typeof(yourClass), "yourMethod")]
public int yourProperty { get; set; }
}
Haven't tried it but it should work.
I have a few of these in my project - some still use regular expressions, but at least this way they're only in one place:
public class TelephoneAttribute : RegularExpressionAttribute
{
public TelephoneAttribute()
: base(#"^\(?(\d{3}\)?)((-| )?\d{3})(-?\d{4})$") { }
}
And more like what your example:
public class MinimumDateAttribute : RangeAttribute
{
public MinimumDateAttribute(string MinimumDate)
: base(typeof(DateTime), MinimumDate, DateTime.MaxValue.ToShortDateString()) { }
}

Inheriting from ViewPage

Is it possible to inherit from both ViewPage and ViewPage<T>?? Or do I have to implement both. Currently this is what I have for ViewPage. Do i need to repeat myself and do the same for ViewPage<T>??
public class BaseViewPage : ViewPage
{
public bool LoggedIn
{
get
{
if (ViewContext.Controller is BaseController)
return ((BaseController)ViewContext.Controller).LoggedOn;
else
return false;
}
}
}
Create both versions:
public class BaseViewPage : ViewPage
{
// put your custom code here
}
public class BaseViewPage<TModel> : BaseViewPage where TModel : class
{
// code borrowed from MVC source
private ViewDataDictionary<TModel> _viewData;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public new ViewDataDictionary<TModel> ViewData {
get {
if (_viewData == null) {
SetViewData(new ViewDataDictionary<TModel>());
}
return _viewData;
}
set {
SetViewData(value);
}
}
protected override void SetViewData(ViewDataDictionary viewData) {
_viewData = new ViewDataDictionary<TModel>(viewData);
base.SetViewData(_viewData);
}
}
then
public class MyCustomView : BaseViewPage
{
}
or
public class MyCustomView : BaseViewPage<MyCustomViewData>
{
}
Depending on how you are doing things you might want to look at
ViewContext.HttpContext.Request.IsAuthenticated
it might save you some time instead of extending the ViewPage class.
If there is some other data that you are after you could maybe write an extension method to one of the classes that provides the data. E.g. if LoggedIn was stored in the session you could extend the context to give you an IsLoggedIn() in method.
Edit:
As your extending a class that is already available in the both the base and strongly typed view it will be available in both. The only other way around is to reimplement the strongly typed version as above.
I wouldn't put this in the View, instead I'd have it as a property on the ViewModel (have a BaseViewModel). It will be easier to test as well as ensuring you're not going down the slope of putting business logic into the views.

Resources