Adding Property in linq to sql designer doesn't update the class - c#

I have a table in sql server called customer_Groups. I dragged that table to a linq to sql designer.
Later I added a field to that table called customers_can_set_delivery as a bit. To reflect this in the linq designer I added a new property to the corresponding data class and set the properties (bit, boolean, name source...)
But when I try to get that field in my code, it doesn't show up with intelisence. Anyone know why that may be

When using Linq to SQL and updating any table from the database, you have to re-drag the table again in the .DBML file. That is, delete the current table object in the file and drag it again. Keep the same name for the table of course.
Don't try to add the new fields by hand, it won't get reflected [by design]. Just delete and drag the table again.
I hope this helps.

The truth is #Diana Nassar is correct in most cases.
My problem was only fixed after restarting sql server.

Related

C# Access SQL ADD COLUMN

I have a problem with the Query Access. My code is as follows:
string query = "ALTER TABLE Student ADD COLUMN Surname MEMO AFTER 'Name'";
Why always inserts the column at the end of the table? Is there any method to insert a new column in a specific position?
First of all, I don't see any reason to add your column to a specific position. You can always use the column order as you want for a select statement for example..
Why always inserts the column at the end of the table?
Because it is designed like that?
There is a method to insert a new column in a specific position?
As far as I know, there is no way to do it without rebuilding your table.
From ALTER TABLE syntax for changing column order
Today when you use ALTER TABLE ADD to add a column, a new column is
always placed as the last column. This is far from often desireable.
Often developers and database designers want to keep some logic in a
column order, so that related column are close to each other. A
standard rule we keep in the system I work with is to always have
auditing columns at the end. Furthermore many graphical design tools
encourage this kind of design, both bottom-end tools like the Table
Designer in SSMS as well as high-end data-modelling tools such as
Power Designer.
Today, if you want to maintain column order you have no choice but to
go the long way: create a new version of the table and copy over. It
takes time, and if not implemented correctly, things can go very
wrong.

LINQ to SQL: Invalid column name 'DepartureGate', even though the column exists

I absolutely do not get this. The column exists in the table, I've ensured that the application is executing the query against the proper table in the proper database, and it still reports that it's an invalid column name.
I'm running NET 4.0, SQL Server 2008 Express Edition. Does anyone have any similar experience?
Executing queries against any other column name in the same table in the same database works extremely excellently. I added this column today and for some reason my application refuses to acknowledge the existence of this column.
Relevant column definition:
Relevant code:
(from x in flightDataContext.FlightDatas
where x.FlightDataId == FlightDataID && x.Departure == true
select new
{
x.ArrivalStationCode,
x.ArrivalStationName,
x.DepartureTime,
x.DepartureGate
}).SingleOrDefault();
I also faced the same problem.You can try the following solutions.
You should update your classes after the database changed(drag and drop your tables to the linq to sql file), so that the column is accessible with object name. if still problem exists then
the value you are saving in the coloumn is greater then the size specified for the coloumn. try varchar(MAX) if your coloumn is of varchar type.
When I encountered this error, I discovered that I had some spurious [Key] attributes on one of my Model objects that wasn't actually part of the unique key definition for that object's table. They had been innocuously sitting there until I refactored another of the object's relationships. Once I removed them, the error about the columns being invalid vanished.
Try this. Before this , delete the table and drag drog the table again on the DBML file and save the DBML.
var flight= flightDataContext.FlightDatas.SingleOrDefault(x=>x.FlightDataId == FlightDataID && x.Departure);
Now you can access:
flight.ArrivalStationCode,
flight.ArrivalStationName,
flight.DepartureTime,
flight.DepartureGate

EF - Import a new table from DB and have it replace an existing one

So I need to update the model from the db. The issue is that I want the new table to replace an existing one.
This is the format of my existing table.
Here is the new table after import it from the DB.
I delete the original table and rename the fields to match, build and get errors.
I am getting errors because it can't find UserProfileTenant. When I use intellisense I see that it has USR_PROFILE_TENANT2 instead of UserProfileTenant.
I'm not sure where I need to update to have names properly updated.
Looking through some questions I found Change DB in EF. But the accepted answer isn't the one I used. Its this one by zeeshanhirani.
Besure to also update the column mappings after the new table is selected.

LINQ to SQL : How to Insert record into a view?

I'm using Linq to SQL for the Database operations, and am trying to perform insert operations in a VIEW, It throws the Error,
XXt threw exception: System.InvalidOperationException: Can't perform Create,
Update or Delete operations on 'Table(XXX)' because it has no primary key.
How to use LINQ to insert a record into View using C#?
Thanks.
You can insert/update into views as per Updatable Views here. Only one underlying table can be inserted/updated to or it will fail. To implement this functionality using LINQ, do the following;
In your .DBML file tag one (or more) of the columns in the view as a Primary Key
Ensure any mappings in the view you are expecting to insert/update are exposed simply as a link to the base table column. Example;
Insertable/Updatable columns cannot include;
SUM(BaseTable.ColumnName) as ColumnName
ISNULL(BaseTable.ColumName,0) as ColumnName
BaseTable.ColumnName1 + ' ' + BaseTable.ColumnName2 as ColumnName
But can include;
BaseTable.ColumnName
BaseTable.ColumnName as MyNewName
Tag any of the columns that are not direct mappings to the base table as Auto Generated Value in your .DBML.
Give it a shot. I am successfully using this technique to use views as the only objects i use for both reading/inserting/updating records.
Actually you can insert into a view..if the underlying view has one table then you can insert into it.
If it has more than one table..then u can use instead of triggers;
Also I have inserted a record into a view..in linq to sql. (i have just started learning linq myself).
I had to create a primary key on a view. using the designer and then set the auto sync for that field to never. that should do the trick..
We can do insert, update and delete operations using a VIEW in LINQ to SQL process. All that we need to ensure is: view should have primary key.
We can set primarykey for a field in the view.
open the .dbml file designer and select the field which you want to make as primarykey and press F4 (open properties window).
select true for Primary Key property of the selected field.
Now execute your program. It should work.
You cannot insert into a VIEW. You can only insert into a table.
You can do this - see below.
Auto-Sync must be 'OnInsert' for this field in dbml.
I assume you mean that instead of trying to insert a row into a View, you are trying to insert a row into a table. You do not insert rows into Views.
Having said that, L2S requires your tables to have primary keys, as the error message indicates. Once you create a primary key, and update your .DBML accordingly, you should be fine.
Randy

LINQ to SQL - The database generated a key that is already in use

I have a simple problem that reads an Excel file (using interop) and fills an MSSQL database file with some data extracted from it. That's fine so far.
I have a Shops table with the following fields:
ID: int, auto-generated, auto-sync: on insert
Name: string
Settlement: string
County: string
Address: string
I read the excel file, and then create a new Shops object and set the Name, Settlement, County and Address properties and I call Shops.InsertOnSubmit() with the new Shops object.
After this I have to reset the database (at least, the table), for which the easiest way I found was to call the DeleteDatabase() method and then call CreateDatabase() again.
The problem is, that after the first reset, when I try to fill the table again, I get the exception: The database generated a key that is already in use.
Additionally, from that point on, I'm unable to use that database file, because DatabaseExists() returns FALSE, but when I call the CreateDatabase() method, it throws an exception, that the database already exists (although the data files don't exist).
What am I doing wrong?
Thank you very much in advance!
It sounds like you are re-using the data-context beyond what is wise. Try disposing and re-creating the data-context after deleting the database.
I suspect the problem is that the identity manager is still tracking objects (destroying and recreating the database is such an edge-case that I think we can forgive it for not resetting itself here).
I encountered this error. I had a log table, with an identity. I was truncating the log, while my application was running. What happened was the DB would start the identity column over again when I truncated, however the data context I was using to log still had objects it was tracking with the same key.
I encountered this error because I was using a custom stored procedure for the insert with a table that had an identity column, but I had forgotten to "SET #Id = SCOPE_IDENTITY" at the end of my sproc.
I wasn't actually using the resulting identity value so the problem only showed up when I inserted two or more rows. Tricky bug.

Categories