DBIx::Class::Schema::Loader using different namespace for table relationships - database-schema

I want
DBIx::Class::Schema::Loader
to create a class (say OurSchemaBase::*) for each of my tables.
Then I want to derive each of these classes and put them in, say, OurSchema::* and I want the relationship to point to my derived class not to the autogenerated base class.
Can I make DBIx::Class::Schema::Loader create relationships in OurSchemaBase::* to OurSchema::* classes instead of to OurSchemaBase::* classes?
Is this possible with DBIx::Class::Schema::Loader?

Nothing related directly to Schema::Loader, check out https://metacpan.org/pod/DBIx::Class::Helper::Row::SubClass.

Related

Why does Windows Identity AspNetRoles have Discriminator

I implemented Windows Identity into my application and using EF Migrations the appropriate [AspNet*] tables were added to my database. As typical in this scenario, I created an ApplicationUser class that inherits from IdentityUser. My 'ApplicationUser' class does not have any additional properties. And at that point in time I did not touch any code or classes related to Roles.
Now I am looking to add roles so I followed the same pattern as before. I created an 'ApplicationRole' class that inherits from 'IdentityRole' but did not add any addition properties.
However, and this is where my question comes in, EF migrations is trying to create a [Discriminator] column for the AspNetRoles table. Yet it never created one for the AspNetUsers table in the previous migration.
I understand the purpose of the Discriminator column so I am not questioning why it is there. I just do not understand why it is adding it for one table and not the other when both base classes were inherited by the "application" equivalent classes.
The only plausible reason I can think of is because the original IdentityRole class was used in the earlier migration and now I am trying to add a new migration that uses the base class. If so, is there a way to get EF to ignore this? There are no plans to ever create multiple classes that inherit from the base nor will I be using the base directly so the Discriminator will always contain the same value.

Do Core Data objects replace standard class objects?

I'm getting into Core Data and have started making "Classes" out of entities. Do I use these classes based off of entities as regular classes with methods etc?
Normally if I was using SQL directly I would have classes for each of my database objects (Tables) that store the working data and perform their functions.
Is this the same with Core Data - do I use the entity-based classes (subclasses of NSManagedObject).
So I might have these entities: Business -> Departments -> Employees
To comply with Core Data - do I make one class for each of these subclassing NSManagedObject and then add methods to it accordingly (like I would an NSObject class)?
Yes, you usually use the Core Data generated classes. You usually don't add methods to those classes, instead you should put them into a Category. This is because if you change an entity, you need to regenerate the class which will erase any methods that you added.
Yes(ish). They are your model classes. You aren't creating them 'out of' entities, they are the code representation of your entities.
You should have one class per entity, but you shouldn't create them yourself. You should get Xcode to create the classes. Or, better yet, use mogenerator.

Poco+Entity Framework 4. Where should I add my methods for working with Poco classes?

I've tried to use Entity Framework 4 and POCO for my MVC 3 project. May be, I don't understand the main idea of this ORM, but the problem is following:
I added ADO .NET Entity Data Model and make model according to database.
I clicked Add Code Generation Item and added ADO .NET POCO Entity Generator.
It makes classes for every database table.
I want to add some methods to work with data (Add, Update, Delete, GetAll etc) to appropriate models.
For LINQTOSQL I added partial classes and placed them to Models. But now I can't do it because:
a) Models folder has classes with the same names, which was created by POCO.
b) If I place my partial class in the another folder, it will be another namespace - so, such classes won't be partial one.
c) If I place my code in POCO classes, it can be destroyed during update POCO.
How can I use it? Where sould I place my methods for data working?
Is the best way to make for POCO and EF the other project - http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx?
First of all you don't have to write your CRUD inside POCO,
There are many places where you can do it like in edmx.cs file or write one more layer which is called as CRUD Services which handles the Database operations using context object.
Now coming to your questions,
Create separate Models folder and place the Model classes in there.
Your Model class may like this,
EmployeeDepartmentModel
{
prop EmpList List(Emp);
prop DeptList List(Dept);
//Emp and Dept are my POCOs
}
So now I have to fill both of these list(Your CRUD question),
For that, I will Create one method in Controller class(its better to write such logic in some another library, but for time being I suggest you to create in Controller),
FillTheModel()
{
EmployeeDepartmentModel.EmpList = EDMX.GetAllEmployees;
EmployeeDepartmentModel.DeptList = EDMX.GetAllDepartments;
}
Now you can bind this model with your view.
You can place the partial classes in another folder and modify the namespace.
I agree with allisewell, but if you really want to add parts to partial classes, give files another name,
e.g. MyPoco.Part2.cs or modify t4 template to name generated files
e.g. Poco.Generated.cs

Entity Framework Bottom-up Inheritance

I use standard ObjectContext and EntityObjects in my application. Let's say two of my tables are Projects & Services. Projects have Subproject (from Projects table with ParentID == ProjectID) and also Services. So I would have a hierarchy like Projects->Subprojects->Services. But I need to inherit Projects and Services from an abstract base class so I can use any of these entities as a new Task/Job entity in my application. Then, for example I can create a TreeList listing all Tasks (either a Project or Service). Is there anyway in EDMX designer I can create a new type (entity) which is the base calss for two or more concrete types?
It is possible with TPC inheritance but it will include a lot of complication to your design. For example:
you will have to move shared properties to the base class
you will probably have to maintain some mappings manually in EDMX (at least I had when I did the sample on screenshot)
you will have only single ObjectSet<Tasks> and you will have to use OfType to query only Projects or Services
you will have to use unique Id per Task = across both Project and Service tables (can be achieved by correctly configured identities in database)
It will look like:
Another option is using interface on your entity objects instead of parent class. You can define interface in your partial part of entity object and handle retrieving both Projects and Services by yourselves where your UI will expect only list of types implementing your interface.
http://blogs.microsoft.co.il/blogs/gilf/archive/2010/01/25/table-per-concrete-type-inheritance-in-entity-framework.aspx
Since it sounds like your data is coming from 2 separate tables, Projects and Services, no, I don't think you can achieve this in the designer (at least, not without hand-editing the generated edmx). If there were a common table to represent the base class, that could be done in the designer, but that doesn't sound like it fits your situation.
What you may be able to do is use an interface instead of an abstract base class, and use partial classes in your entity model to implement the interface for each of your entities. You can't directly inherit from your abstract base class in your entity model, because all of your entities already derive from EntityObject. If you have a lot of shared implementation that resides in your base class, it might be worthwhile to switch to POCO, where you can define your own inheritance hierarchy.

Creating POCO Classes on Phasis

I create persistence ignorant classes for an existing DB which is very big and i don't want to create all classes in the beginning i want to create only classes i need and create each class when i need it.
Is there any feature in Entity Framework can do this?
You can build incrementally your EDMX file adding only entities you need to use at the moment. Than you need to define only POCO classes defined in your EDMX file or you can use POCO template which will generate POCO classes for you.