Here is my tables structure..
Profile: ProfileID (PK), ProfileName varchar(50), GenderID (Fk)
Gender: GenderID (PK), GenderName varchar(50)
Gender table has 2 possible values : Male, Female.
In entity framework, when I am updating the profile with GenderID, I use the following code:
profile.GenderID = Repository.
GetGender(
Request.Form["Profile.GenderName"].ToString()
).GenderID;
Repository.Save();
GetGender method looks like the following:
public Gender GetGender(string genderName)
{
return (from gender in db.Genders
where (gender.GenderName.Equals(genderName))
select gender).First();
}
Is there a better way of doing it? I feel like I am not using Entity Framework like it should be...
If I assign the value to profile.Gender.GenderID as opposed to profile.GenderID then I am updating the original Gender Lookup table which is not what I want.
I am confused..
Thanks..
If the only thing you have to look up the gender is the text description, then that's the best you're going to do (although I would probably add StringComparison.OrdinalIgnoreCase to the Equals). If you have the PK, there are other options.
It seems like a lot of effort to keep calling the database for this information. Could you not retrieve the pairs of values once, for example in Application_Start, put it in Cache, and then reference it as needed? After all, the values are not likely to change very often.
Related
I've 2 sails model
1) student
id - pk
name
revision
2) studentDetails
id
student_id -> fk
student_reviosion
Whenever student gets updated, revision is automatically increment by 1. How I can make sure that revision and student_revision should be sync in both table? Is there any way to define sails_model like that?
I know this can be achieve through lifecycble callback and would like to know if anything that we can directly achieve by defining some relation in sails models itself.
You should choose just one of these tables to store the data in. Since you are creating a relation, you will be able to access the data when you need it. For example if you keep revision in student, when you look up student details, you can do something like let studentDetails = await StudentDetails.find({id: id}).populate('student') [I am naming the foreign key to 'student' because it will make more sense when you populate it like this].
Then you can access all the information from the associated student record, like studentDetails.student.revision
https://sailsjs.com/documentation/concepts/models-and-orm/associations/one-to-one
Its a simple question.
I have designed a project with a normalized database schema.
I am using code first.
I have a table called JOBS. I have another table called status. I intend to give a job a status. The status has to be user set so its in a table which you can add to. A job can be, say, "booked", "parts to be ordered", "invoiced". etc
As a POCO class I have the table STATUS with the following:
statusID (PK)
statusName
description.
In the JOBS table I would have a column called statusID as the foreign, non null key. EG
jobsID (PK)
clientID (FK)
statusID (FK)
jobTypeID (FK)
.
.
My question is, can I do this? Can I have a number of statelite tables (client, status, jobType) with names and descriptions and in the JOBS table there is a column with the satelite tables primary key as the column name?
I am confused as to what relationship this represents (one to one - one to many) and whether you can use the primary key of one table as the foreign key in the JOBS table when approaching this using Code First. Its not overly clear in the tutorials I have worked through.
Bottom line I want a specific job and be able to attach a specific status to it and in the view show the job with that status name and description...
Hoping someone might clear up the mud..
Simon
I think what you want to do are simple one-to-many relationships.
You have already defined this and everything looks fine for me.
But you can also add the Navigation property to your status class to make this one-to-many relationship more clear:
public List<Job> Jobs { get; set; }
I simply cannot wrap my head around Models.
I have two tables, users and companies. Users contains a column called Company which is a foreign key to the companies table. the companies table has two columns, an ID and company_name The value of Company under users references the id in Companies.
I am trying to get the value of company_name through the foreign key like so.
$user = User::find(1)->company->company_name;
My thought process, which may be wrong, is that this (depending on if models are correct) should get the value of the Company column and then using that value, get the company_name from the Companies table.
My models look like so
User
public function company() {
return $this->belongsTo('App\Company', 'company');
}
Company
protected $table = 'companies';
public function user() {
return $this->hasOne('App\User');
}
All I continuously get is Trying to get property of non-object
I can see in the error log it gives that it's actually getting the company name too!
Trying to get property of non-object', 'C:\wamp\www\laravel\app\Http\Controllers\HomeController.php', '41', array('userid' => '1', 'usercompany' => '1', 'company' => 'BMW'))
But I don't understand where it's pullyig userid and usercompany from.
What am I doing wrong?
In this case, you're trying to get the property of a non-object, meaning that either User::find(1) or company is not an object. In the error message provided, it looks like one of the two is an array.
Look at your underlying database tables. Generally, the id is a protected field that is not returned in the result object. usercompany looks like the concatenated field to cross-reference the two tables (i.e. the foreign key).
Let's say I have two tables: table Category and table Book
I would like to add a Category, then use its ID for inserting a book (which has a CategoryId field pointing to the table Category).
To get this info, I now commit my changes after inserting the category as it is not provided otherwise.
Is there a way to point to this category when inserting my book without commiting after inserting the category?
Thanks in advance
If the ID is generated by the database, then no, you can't get it until it is committed
It sounds like you have a FK relationship setup. If that's the case, if you associate the Category and Book entities together - EF should be create the necessary FK's.
For example:
var Category = new Category();
Book.Category = Category;
context.Books.Add(Book);
context.SaveChanges();
Would create a book, and a category and depending on how your mapping is setup the proper fields should be set. Can you post your model if this is not the case?
I'm using Ebean, and define such a model:
#Entity
#Table(name = "users")
public class User extends Model {
#Id
public String id;
public String email;
public String name;
}
You can see the field id is String, and has a #Id annotation.
Now I save it:
User user = new User();
user.id = "abc";
user.email = "a#a.com";
Ebean.save(user);
But when I saved it, I found the value of it's id is: 1, not abc I specified.
I checked the sql, found the table generate:
create table users (
id varchar(255) not null,
email varchar(255),
name varchar(255),
constraint pk_users primary key (id))
;
create sequence users_seq;
You can see there is a seq users_seq which has been used when inserting a user.
How to define the model or how to configure Ebean to let it not do anything to the #Id field? Just let it use my specified value?
===========
UPDATE
Sorry, guys, I found this strange behavior is because of my mistake!
Yes, I use it with playframework 1, and I tried to create a play-ebean module for myself. But there is something wrong: When I save a model, I cleared the id value by mistake!
So the assigned value abc is missing, then Ebean will try to use a seq to get a new value for it.
Thanks for all you help, and sorry again, I will be more careful when I ask question next time.
Isn't it better idea to create another unique field and use it optionally ie. String userCode or something?
Play with Ebean uses auto incrementation of Id's to make sure the Id is unique so I'd personally didn't change that as all docs assumes that id of model is some numeric kind.
You can thought use Play's routes to find user by its string-id and still use Long id to perform basic operations.
(I'm writing in Play's context as I assume that you ask in it ;) )
You need to extend GenericModel instead of Model if you want to operate on your own #Id field. I am also talking in PlayFramework context.