Entity Framework question

M

mazdotnet

Hi,

I just started playing around with EF today and it seems great (faster
development too once I get it right). I'm running into the following
issue,

I have 2 tables
'Countries' Table
CountryId
CountryName

'CountryStateProv' Table
StateProvId
CountryId
Name

So far I got to work for when I select a country, it shows all the
states that fall under that country. Now I want to take a state
(identified by StateProvId) and assign it to another country (just for
learning purposes).

MyDBEntities1 MyContext = new MyDBEntities1();
List<CountryStateProvince> stateList = new List<CountryStateProvince>
();

// Find the entry by StateProvId and change the CountryId which is not
visible in the class
CountryStateProvince countryStateProvince =
MyContext.CountryStateProvince.First(c => c.StateProvId ==
StateProvId);
I want something like
countryStateProvince.Countries.CountryId =
some_country_id;
but I get 'Object reference not set to an instance of an object.'

How do I accomplish this? How do I take an existing state defined for
ex. canada and move it to country 'USA' instead?


In another method I have this
ObjectQuery<CountryStateProvince> stateQuery =
MyContext.CountryStateProvince.Where("it.Countries.CountryId ==
@CountryId", new ObjectParameter("CountryId", CountryId));

Which works fine.

Thanks
Maz
 
M

Mr. Arnold

mazdotnet said:
Hi,

I just started playing around with EF today and it seems great (faster
development too once I get it right). I'm running into the following
issue,

I have 2 tables
'Countries' Table
CountryId
CountryName

'CountryStateProv' Table
StateProvId
CountryId
Name

So far I got to work for when I select a country, it shows all the
states that fall under that country. Now I want to take a state
(identified by StateProvId) and assign it to another country (just for
learning purposes).

MyDBEntities1 MyContext = new MyDBEntities1();
List<CountryStateProvince> stateList = new List<CountryStateProvince>
();

// Find the entry by StateProvId and change the CountryId which is not
visible in the class


Well, if you want to change the CountryId, then you must break the
association between the parent to the child or 1-2-many relationship
between the two entities, to expose CountryId on the child entity.

And you're going to have to build the model from scratch to do it.

Building the model from scratch, do the following:

1) A child entity must be placed on the model first, to expose all
fields of the entity on the model. The child entity must be on the model
before it associated parent entity.

2) You will do this one entity at a time of selecting a child entity and
placing it on the design pane. You will use the Update Model by right
clicking the open area in the pane and going back to the select the next
entity to be placed on the pane. You should remember that all child
entities must be on the design pane first before its parent entity to
expose all the child entity's fields.

You DO NOT select multiple entities to be placed on the pane, otherwise,
fields will be hidden.

3) You select the parent and place it on the pane.

4) You save your work and come out of the VS.

5) You navigate with Explore to the location where the Model.edmx is
located, and you will edit it with NotePad.

6) You will delete all <AssociationSet> </AssociationSet> tags and their
data out of the Model.edmx. Those tags are right at the top of the file.
You delete those tag and no others and save the file.

7) You go back to VS, to the project and to the Model design pane.

8) You will delete all association lines off of the model by right
clicking the line and deleting the line, delete all of the lines.

9) You will right-click the designer pane and Validate the model.

10) If it validates, then everything is ok.

If you want complete control of the model and the entities on the model,
exposing all fields/properties of child entities, then that's what you
have to do.

The associations of the tables are still there on the underlying database.
CountryStateProvince countryStateProvince =
MyContext.CountryStateProvince.First(c => c.StateProvId ==
StateProvId);
I want something like
countryStateProvince.Countries.CountryId =
some_country_id;
but I get 'Object reference not set to an instance of an object.'


var countryStateProvince = (Context.CountryStateProvince.Where(a =>
a.contryid == 2).FirstorDefault;


If (countryStateProvince != null)
{
countryStateProvince = 3;
context.SaveChanges();
}

If you want speed in querying the Model, then you use Entity SQL (ESQL),
which not unlike T-SQL, but it's against the model.


If you have to add more entities to an existing model, then you have to
remove the associations of the model again, because they all will be put
back upon updating the existing model with new entities.

HTH
 
M

Mr. Arnold

mazdotnet wrote:

<snipped>
<correction>

If (countryStateProvince != null)
{
countryStateProvince.countryid = 3;
context.SaveChanges();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top