Did I mess up the normal table creation by implementing my own mapping to the our existing user table? When running the application the normal AspNet tables are not generated in the database.
I was able to figure out how to map the AspNetUsers table to our existing table in our database using the following code:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("DBUser");
var entity = modelBuilder.Entity<ApplicationUser>();
entity.HasKey(x => x.UserID);
entity.Ignore(x => x.Id);
entity.Ignore(x => x.PhoneNumber);
entity.Ignore(x => x.PhoneNumberConfirmed);
entity.Ignore(x => x.EmailConfirmed);
entity.Ignore(x => x.LockoutEnabled);
entity.Ignore(x => x.LockoutEndDateUtc);
entity.Ignore(x => x.PhoneNumber);
entity.Ignore(x => x.PhoneNumberConfirmed);
entity.Ignore(x => x.SecurityStamp);
entity.Ignore(x => x.TwoFactorEnabled);
entity.Ignore(x => x.AccessFailedCount);
entity.Property(x => x.UserID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
entity.Property(x => x.PasswordHash).HasColumnName("Password");
}
However, when I run the application and register a user for the first time, the normally created AspNet prefixed tables like UserClaims, UserLogins, UserRoles, and Roles are not created for some reason, and it does insert a record into my DBUser table with the correct information.
Since I'm using an existing database, should I assume that I need to manually create these tables?
Any help with this would be greatly appreciated.
Related
so I have this big table that I want to query and it's so slow, I got a tip that using select is more efficient than include. so how can I convert include to select, in this big query.
ProductEntity? i = await _context.Set<ProductEntity>()
.Include(i => i.Media)
.Include(i => i.Categories)
.Include(i => i.Locations)
.Include(i => i.Reports)
.Include(i => i.Comments)!.ThenInclude(x => x.LikeComments)
.Include(i => i.Bookmarks)
.Include(i => i.Votes)
.Include(i => i.User)!.ThenInclude(x => x.Media)
.Include(i => i.User)!.ThenInclude(x => x.Categories)
.Include(i => i.Forms)!.ThenInclude(x => x.FormField)
.Include(i => i.Teams)!.ThenInclude(x => x.User)!.ThenInclude(x => x.Media)
.Include(i => i.VoteFields)!.ThenInclude(x => x.Votes)
.AsNoTracking()
.FirstOrDefaultAsync(i => i.Id == id && i.DeletedAt == null);
return new GenericResponse<ProductEntity>(_mapper.Map<ProductEntity>(i));
and this is getById method, which just takes one row, I have a getAll that returns a list of products just like this.
The most performant code is always the code that does the least amount of work.
It's possible that what was meant by the 'tip' that "using select is more efficient" may have more to do with answering the question: "do you really, really, really need all that data to be returned?".
Consider, for every .Include you have, EF will be making it's own query to the database to return whatever records are linked by the foreign keys. It's actually pretty efficient on doing that either on the DB side, or in memory if it already has the connected records in memory. (depending on your caching configuration) However, it's going to return the entire Entity referenced. Which may be fine. But, what if each of those 11 sub-entities plus the 7 sub-sub-entities that you have connecting there are each rather large. Do you really, really, really need to return all of that data? Or could you return smaller objects using something like:
var mySmallerObject = await _context.Set<ProductEntity>()
.Include(i => i.Media)
.Include(i => i.Categories)
. . .
.Select(_ => new SomeMuchSmallerObject {
SomeFieldOnProduct = _.SomeFieldOnProduct,
TheOnlyThingINeedFromMedia = _.Media.TheOnlyThingINeedFromMedia,
TheOnlyCategoryFieldINeed = _.Categories.TheOnlyCategoryFieldINeed,
. . . // etc
})
UPDATED
Note: You will still need to either keep all those .Include()s to let EF implicitly build the joins, or else convert them to explicit .Join() statements. By making a smaller object that you use in the .Select() statement, EF will translate this request into a much more optimized query to the DB that only selects the few fields you actually need, and not even return any of the unwanted fields from the database. This also allows you to take full advantage of any indexes you have on the underlying tables in the DB.
Just a thought.
I want to perform bulk update of users with a Approved users, the table
field_user_status_value
-----------------------
entity_type, entity_id, field_user_status_value
The entity_id is the user id which does not exist in the table, below is the custom module I wrote to update the table:
function bulkapprove_action_info() {
return array(
'bulkapprove_action_callback_name' => array(
'type' => 'user', // Can be file, term, user, etc.
'label' => t('Approve User'),
'configurable' => FALSE, // Doesn't need config form
'behavior' => array('view_property'), // Uses view access rights ,
'pass rows' => TRUE,
'triggers' => array('any'), // Works always
),
);
}
function bulkapprove_action_callback_name($entity, $context)
{
db_update('field_data_field_user_status')->fields(array('field_user_status_value' => 'Approved'))->condition('entity_id', $context->entity_id)->execute();
}
But it is not inserting the values in this table
In Drupal you do not want to update the database fields directly unless you created the table. Drupal's internal APIs provide a collection of tools to ensure you update the values correctly and that all supporting modules get notified of changes as needed through the hook system.
In this case the callback gets the actual entity to run your action against (in this case the user object). You want to take action on that entity and then save the entity.
function bulkapprove_action_callback_name($entity, $context)
{
$entity->status = 1;
entity_save('user', $entity);
}
I'm trying to rename my Identity 2.0 tables to have my app name before them. So I've overridden OnModelCreating:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("appname_Users");
modelBuilder.Entity<IdentityRole>().ToTable("appname_Roles");
modelBuilder.Entity<IdentityUserClaim>().ToTable("appname_UserClaims");
modelBuilder.Entity<IdentityUserLogin>().ToTable("appname_UserLogins");
modelBuilder.Entity<IdentityUserRole>().ToTable("appname_UserRoles");
}
I deleted the Migrations folder, made sure these tables did not exist, and ran:
enable-migrations -Force
update-database
When I try to login to the site, it still says it can't find dbo.AspNetUsers.
When I check the migration script, I do see the following:
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.appname_Users", t => t.Id)
.Index(t => t.Id);
Where is it even getting the idea that it needs AspNetUsers? I've scoured documentation and can't find out what's going on. Any help would be appreciated.
Delete all your identity tables from database manually. Then in onModelCreating insert
modelBuilder.Entity<ApplicationUser>().ToTable("appname_Users");
modelBuilder.Entity<IdentityUserRole>().ToTable("appname_UserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("appname_UserLogins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("appname_UserClaims");
modelBuilder.Entity<IdentityRole>().ToTable("appname_Roles");
if you create custom identity classes then use
modelBuilder.Entity<ApplicationUser>().ToTable("appname_Users");
modelBuilder.Entity<ApplicationRole>().HasKey<string>(r => r.Id).ToTable("appname_Roles");
modelBuilder.Entity<ApplicationUser>().HasMany<ApplicationUserRole>((ApplicationUser u) => u.UserRoles);
modelBuilder.Entity<ApplicationUserRole>().HasKey(r => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("appname_UserRoles");
This way works for me.........
In this link http://www.voip-info.org/wiki/index.php?page_id=1735
[settings]
<family name> => <driver>,<database name>~np~[~/np~,table_name~np~]~/np~
sippeers => mysql,asterisk,sip_peers
sipusers => mysql,asterisk,sip_users
queues => mysql,asterisk,queue_table
queue_members => mysql,asterisk,queue_member_table
meetme => mysql,asterisk,meetme_table
voicemail => mysql,asterisk^
Can't the family name by anything as we wish? Can I have sipfriends as a family name?
You can use ANY table for example like this
sippeers => mysql,asterisk,sipfriends
sipusers => mysql,asterisk,sipfriends
Where sipfriends can be table or view(peer update will not work unless you do ONUPDATE trigger).
If you want class enother name, for example something like this
sippeers => mysql,asterisk,sipfriends
sipusers => mysql,asterisk,sipfriends
sippeers2 => mysql,asterisk,sipfriends2
sipusers2 => mysql,asterisk,sipfriends2
then you need do changes in chan_sip.c to lookup both classes.
So i have a GUI for a simple database made with Symfony2 + Sonata Admin Bundle + Doctrine 2. That database will hold billions of rows so the dates are stored as timestamps (to save space) but in the GUI displayed as dates (ex: 2013/10/17 10:05:06). Everything works in the GUI except the filtering by dates. I tried all sorts of configurations in the class that extends the Admin, method configureDatagridFilters(). I cannot make it to work... can you help me?
I found a work around that i think it can be useful in many other situations as well:
So in the class that extends Admin.php we have the method configureDatagridFilters(). There one can add an input like
->add('startDateFrom', 'doctrine_orm_callback', array(
'label' => 'Start Date from',
'callback' => function($queryBuilder, $alias, $field, $value) {
if (!$value['value']) {
return;
}
$inputValue = $this->dateStringToTimestamp($value['value']);
$queryBuilder->andWhere("{$alias}.startDate >= :startDateFrom");
$queryBuilder->setParameter('startDateFrom', $inputValue);
return true;
},
'field_type' => 'text'
), null, array('attr' => array('class' => 'calendar',
)))
As you can see, in this way we can manipulate the field value as we wish. I hope it will help others too :)