ASP.NET Codefirst relationships - asp.net-mvc

I made this relationships in codefirst but it shows exactly this error that shows in this image. I upload my models for you and you can download it from here[4kb]. what is this error? why it shows this? I want to have all my categories in posts table. so what is way? who can help?
someone tell me what is wrong?

By convention in Entity Framework, deleting an entity will delete anything that is linked to that antity. In your case that means deleting cat1 will delete every cat2, cat3 and post that is referencing cat 1.
Your problem occurs because you specify WillCascadeOnDelete(true) on the posts map that you also want a post to automatically trigger a deletion of the cat1, cat2 and cat3 that the post references which can cause a partial or complete wipe of the database (posts delete categories that delete posts that delete categories and so on).
To fix it you need to remove your WillCascadeOnDelete(true)-mappings from postMap.
this.HasRequired(t => t.cat1)
.WithMany(t => t.posts)
.HasForeignKey(d => d.excat1);
this.HasRequired(t => t.cat2)
.WithMany(t => t.posts)
.HasForeignKey(d => d.excat2);
this.HasRequired(t => t.cat3)
.WithMany(t => t.posts)
.HasForeignKey(d => d.excat3);
You need to add WillCascadeOnDelete(false) to the categories higher up the tree to your cat2Map and cat3Map.
cat2Map:
this.HasRequired(t => t.cat1)
.WithMany(t => t.cat2s)
.HasForeignKey(d => d.excat1)
.WillCascadeOnDelete(false);
cat3Map:
this.HasRequired(t => t.cat1)
.WithMany(t => t.cat3s)
.HasForeignKey(d => d.excat1)
.WillCascadeOnDelete(false); <-- Add this
this.HasRequired(t => t.cat2)
.WithMany(t => t.cat3s)
.HasForeignKey(d => d.excat2)
.WillCascadeOnDelete(false); <-- Add this

Related

EntityFramework - Port WithRequired to EF Core

I'm porting an application from MVC5/EF6 to MVC6/EF7, but having an issue with this particular line:
modelBuilder.Entity<Client>().HasMany(c => c.Payments).WithRequired(e => e.Client).WillCascadeOnDelete(false);
Apparently the WillCascadeOnDelete is transformed to the OnDelete with restrict as parameter, but I can't find any documentation on the "WithRequired" part which has disappeared too in EF7. Has 'WithOne' the same impact or am I completely wrong here :
modelBuilder.Entity<Client>().HasMany(c => c.Payments).WithOne(e => e.Client).OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);
You are correct in both cases. Here is a detailed post... http://www.mikee.se/posts/migrating_from_ef6_to_ef_core
Typically these mappings would change from this in EF6.
x.Entity<Company>()
.HasMany(c => c.StatementOfEarnings)
.WithRequired(e => e.Company)
.WillCascadeOnDelete(false);
To this in EF Core
x.Entity<Company>()
.HasMany(c => c.StatementOfEarnings)
.WithOne(e => e.Company)
.OnDelete(DeleteBehavior.Restrict);

Route match to dynamic actions in rails

Can I write one row route for below two rows?
match "/article/:id/" => "articles#redirect"
match "/articles/:id/" => "articles#redirect"
I tried something like
match "/:article_redirect/:id/" => "articles#redirect", :constraints => {:article_redirect => /[article|articles]/}
But it didn't work.
I think your regexp id not good: try
match "/:article_redirect/:id/" => "articles#redirect", :constraints => {:article_redirect => /(article|articles)/}
that is (article|articles) instead of [article|articles]

Rails - Dynamic routing based on host & ID

I have a rails application that contains many user pages. When a user wants to point a domain at this page, how would I do that?
Right now I've tested out this, and it works -
root :to => "controller#show", :id => 4, :constraints => {:host => "www.exampleurl.com"}
but need to convert this to be dynamic, so that after I migrate a column into the model called
domain it checks domain and serves it the proper ID.
something like -
root :to => 'controller#show', :id => ':id', :constraints => {:host => ':domain'}
What would something like this look like?

Zend Framework 2 - BjyAuthorize always denies access

I have setup the bjyoungblood/bjy-authorize module, but I am currently getting a 403 "access denied" error for each URL except for the one configured in the home route.
My module.byjauthorize.global.php looks like following:
'bjyauthorize' => array(
'guards' => array(
'BjyAuthorize\Guard\Controller' => array(
array('controller' => 'index', 'action' => 'index', 'roles' => array('guest','user')),
array('controller' => 'index', 'action' => 'stuff', 'roles' => array('user')),
array('controller' => 'zfcuser', 'roles' => array()),
//backend
array('controller' => 'Application\Controller\Index', 'roles' => array('admin')),
array('controller' => 'MyModule\MyEntity\MyEntity', 'roles' => array('admin')),
),
'BjyAuthorize\Guard\Route' => array(
array('route' => 'zfcuser', 'roles' => array('user')),
array('route' => 'zfcuser/logout', 'roles' => array('user')),
array('route' => 'zfcuser/login', 'roles' => array('guest')),
array('route' => 'zfcuser/register', 'roles' => array('guest')),
array('route' => 'home', 'roles' => array('admin')),
array('route' => 'my-entity', 'roles' => array('admin')),
),
),
),
I tried deleting the BjyAuthorize\Guard\Route part, but with no effect.
When I remove the home route then the homepage is also blocked.
So both Controller- and Route-Guard seem to work.
How can I debug this behavior?
NOTE: following is valid for BjyAuthorize 1.2.*
First of all, consider that protecting both the routes and the controllers is unnecessary. I personally always protect the controllers only, since there may be multiple routes to a same controller.
Once you removed either the route or the controller guard's config, you can:
Install Zend Developer Tools, which allows you to have an overview of the currently set Acl role, like in this picture:
Check if you have configured the correct identity provider: the default one uses ZfcUser's user id and looks up his role in the user_role table.
Check that the guest role has access to the public pages, such as the zfcuser controller (for login actions) or the zfcuser/login route.
As Akrabat pointed out, the configuration for the BjyAuthorize\Guard\Controller and BjyAuthorize\Guard\Route are whitelists, which basically means that you have to setup access for the default guest role if you want to browse pages being un-authenticated.
As soon as a guard is configured, it blocks access to any not configured resource, so be sure that you have granted the role guest (or whatever you configured in $config['bjyauthorize']['default_role'] access at least the login controller or route.
As soon as you create one entry in the 'BjyAuthorize\Guard\Controller' array, then you need to create entries for every controller with permissions as appropriate.
I have this:
'BjyAuthorize\Guard\Controller' => array(
// Access for everyone
array('controller' => 'zfcuser', 'roles' => array('guest')),
array('controller' => 'Application\Controller\Index', 'action' => 'index', 'roles' => array('guest')),
array('controller' => 'error', 'roles' => array('guest')),
// Restricted
array('controller' => 'User\Controller\AdminUser', 'roles' => array('admin')),
),
It's important that you give guest access to zfuser (for logging in!) and error (hard to debug stuff otherwise).
I've not tried using controller and route guards simultaneously.
I had the exact same issue.
I think the problem is that BjyAuthorize is not well documented so many of us are simply copying and pasting and working out from the files provided. For instance from the following:
'BjyAuthorize\Guard\Controller' => array(
array('controller' => 'zfcuser', 'roles' => array()),
),
You would expect to add your controllers as such:
array('controller' => 'controllername', 'role' => array()),
However you need to add the full path otherwise it will not work:
array('controller' => 'Folder/Controller/Action', 'role' => array()),
I hope this saves someone a few hours work as I was totally befuddled by this!
debug your code by this in module.php
public function onBootstrap($e)
{ echo "<pre>";
var_dump($e->getTarget()->getServiceManager()->get('BjyAuthorize\Provider\Identity\ProviderInterface'));
}

Point top-level domains to individual records in a rails app

Is there a way to point top-level domains to individual records in a rails app - similar to subdomains.
kinda like http://wordpress.org/extend/plugins/wordpress-mu-domain-mapping/
You want to look at constraints and the request object:
http://edgeguides.rubyonrails.org/action_controller_overview.html#the-request-object
match '/' => {:controller => "mycontroller", :action => "show", :id => "1"},
:constraints => { :domain => "mydomain.com" }
(Remove the line-break if this doesn't work)

Resources