SubSonic2.2 Add() not working - collections

I have 2 tables in my DB, Customers, Contacts. CusID is a Foreign Key in Contacts table. I tried the following
Dim contact as New Contact(Guid.NewGuid())
contact.FirstName = "Mary"
contact.LastName = "Jane"
customer.Contacts.Add(contact)
customer.Save()
Customers and Contacts classes were generated with SubSonic2.2. The contact is not being saved in the DB.

Maybe the answer is a little bit late but maybe that helps anyway.
Subsonic's Save() method only persists the current object by design.
You can either do:
customer.Contacts.SaveAll();
or
customer.DeepSave();
instead.

Related

Relationships across multiple Realms?

Realm:
We have the following scenario: There are several stores with employees and customers, several employees that could work at more than one store, and several customers that may shop at several stores. This could be represented with these classes
class Store {
dynamic var id = ""
dynamic var address = ""
let workers = List<Employee>()
let customers = List<Customer>()
}
class Customer {
dynamic var id = ""
dynamic var name = ""
let stores = LinkingObjects(fromType: Store.self, property: "customers")
// ... many more fields about this customer
}
class Employee {
var id
var name
let work = LinkingObjects(fromType: Store.self, property: "workers")
}
The catch here is that we must protect customer information, so none of the customer info can be present in a shared realm and needs to be secure. Neither the store nor Employee data is a security matter. Our current approach is to give each customer their own realm, however, the major drawbacks to this is that requires massive duplication since each customer realm must copy the data of the store. The other drawback is that we would be copying customer data into a shared realm which is a security risk. What would be the best way to architect this scenario that allows for relationships across different Realms?
Realm doesn't currently support "direct" object links across Realms analogous to object properties within the same Realm.
Instead, what I suggest you do is to give your objects primary keys (you can probably just declare your existing id fields as such, or create a new internalId field if your existing id field can't be used for this purpose).
Primary keys are mandatory, must be unique, and can't be changed after they are set, which makes them great for uniquely identifying objects. Our documentation discusses them in greater detail.
Then, instead of directly storing customer info/a customer object in a shared Realm, you can just store the primary keys for the relevant customers, for example in a list. (Right now you'll have to make a wrapper CustomerKey object for example to store the customer's primary key, but we plan to support collections directly containing strings or other primitive types very soon.)
You can enhance this further by adding helper methods on your objects that can be passed in a customer Realm and return the user object (or whatever object's primary key is being stored), looking it up in the Realm automatically. You can use Realm's object(ofType:forPrimaryKey) method to look up an object based on its primary key.
The main limitation is that you won't get the automatic updating of links you would get with object, list, and LinkingObjects properties. You'll have to manually perform the bookkeeping yourself.
If you have ideas for functionality you want to see in Realm that would go beyond what I've posted here, feel free to share your thoughts at our GitHub issue tracker. We welcome feature requests.

Entity Framework One-to-one or zero - Database first

I'm trying to create a setup where I have different entities (e.g. User and Customer). Each of these entities have some Login-information which I store in a Login-table to avoid having to do duplicate work on the different entities requiring Login-data.
However - I can't get the setup to work using Entity Framework. I hope you can help me out :)
My database has 3 tables:
Users
Id (PK)
Name
...
Login_Id (FK to Login.Id)
Customers
Id (PK)
CustomerNumber
...
Login_Id (FK to Login.Id)
Logins
Id (PK)
Username
Password
...
Each entity has one - and only one - login. So a given User has one Login. And a given Customer has one Login.
I need to be able to do something like this in C#:
var user = _userService.GetUser(2);
user.Login.Username;
var customer = _customerService.GetCustomer("abc");
customer.Login.Username;
But I simply cannot get the EF mappings to work.
I've tried changing my database schema so that Logins contains nullable User_Id and Customer_Id columns - but the result is the same. My Login-entity is null when accessing User or Customer. And I've already added the
Include(i => i.Login);
I might have just gone totally blind looking at it. I've read dusins of posts about this - but the majority of them seems to focus on Code-First and not Db-First (e.g. http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-one-to-one-mapping/)
Could someone please supply me with the EF Fluent API mappings to get this to work?
Thank you guys so much! :)

Using composite key in AspNetUsers table in MVC 5 application

I'm purposely not posting any code here as I'm really just looking for some guidance to the following problem:
I have created a three new fields in my AspNetUsers table - FirstName, LastName, and NickName. I also created a composite key using those three fields. When I try to create a new user that has all three of these fields the same, the application throws an error as expected when the unique rule is violated.
I would like it to simply post back to the Register User form indicating that the NickName must be changed to something else.
Do I need to implement a Custom User Store? Is there a simpler way?
Thank you for any suggestions.

aspnet_user table for storing customer information

When regsitering in my site (ASP.Net MVC application), the users get inserted into the aspnet_users table. Since its a shopping site, I would want the users to have a customer id and all their details provided by them at registration in this Customer table as well. How do I link these 2 tables? Is it recommended to use the aspnet_user's UserId(Guid) in the application for other business processes.
Also, I would like to know when should a new record be inserted into the customers table.
I mean, when should a new customer be created. I guess its not good to create a record as ans when users are registered? Here, I want to know whats the norm? I felt it would be better to add it when a user adds an item to the shopping cart. Pls guide me.
Thanks in advance.
Add the UserId field into your customer table and then make a foreign key relationship back to the UserId in the aspnet_users table if you want to enforce relational integrity.
I'm not sure what you mean about when to insert the customer record. As long as you insert it after you have created the user (so that you have the user ID), you should be fine. It can happen in the same postback.
I'm not sure how you are saving the user. As in are you using one of the built-in ASP.Net controls or making the call manually?
If you are using the Membership provider as it sounds like you are, you can save the member using:
var user = Membership.CreateUser;
Guid userKey = user.ProviderUserKey;
//Populate your customer object.
//now use whatever EF/ADO/etc... to save your customer record.

get current logged in userID in aspnet mvc membership

i am trying to show a list of users of my application "school", when admin logs in then he can view all the users in list but when school principals logins, he should get only users of his school, So i thought to get the current loggedIn userId first and then by that userId i'll get schoolId since userId is foreign key in school table...once i'll get the schoolId i can show the members of that school.
But my problem is how to get the UserID of currently loggedIn. I'm using MVC 1.0 Asp.Net -- "Membership"
if my logic above is wrong then please tell me the alternate good idea, so that principal can see only his users list..
Based on this question and answer by J. Pablo Fernández you can get the user id of the current user with the following code:
//It will only be a Guid if you are using SQL Server as the DB as oppose to MySQL
Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
Here is the MSDN Documentation.
The Simple Solution
With the scenario you describe you would only be able to do this by retrieving the User information with the data stored in the HttpContext.Current.Identity. For example, if the HttpContext.Current.Identity.Name is the Username, then you would use that value to retrieve the User data for the principal that should include the UserId you can use to locate the appropriate school.
Alternate Solution
You might consider storing the SchoolId in the user's profile so that it is more easily accessible.
The easiest way that I've tried
You have to include Microsoft.AspNet.Identity
using Microsoft.AspNet.Identity;
Then use it like this
var userId = User.Identity.GetUserId().ToString();

Resources