I'm trying to do a basic lnq to sql foreign key mapping using attributes. This should be really simple but I'm having a hard time finding decent info on it on the net. Where am I going wrong?
Say for example you have a class named User with UserId, FirstName, LastName, Location on it. Location is an object called Location
Location class has LocationId, StreetNum, Name, Suburb
How do I map that with Linq to Sql?
Here's what I'm trying
[Column]
public int LocationId { get; set; }
private EntityRef<Location> _location;
[Required(ErrorMessage = "Please enter your suburb")]
[System.Data.Linq.Mapping.Association(Storage = "_location", ThisKey = "LocationId", IsForeignKey = true)]
public Location Location
{
get { return this._location.Entity; }
set { this._location.Entity = value;
LocationId = value.LocationId;
}
}
I'm getting this error:
The null value cannot be assigned to a member with type System.Double which is a non-nullable value type.
Can anyone help?
Probably, somewhere you've used double datatype (StreetNum, maybe).
In the database corresponding column is marked with NULL flag. Try to use double? or Nullable<double> datatype for your properties.
Related
I am trying to read data from my database. I have created a table called ForumSections, here is its table definition.
CREATE TABLE [dbo].[ForumSection] (
[Id] INT IDENTITY (0, 1) NOT NULL,
[SectionName] NVARCHAR (200) NOT NULL,
[TopicsinSection] INT NOT NULL,
[SectionDescription] NVARCHAR (MAX) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
When I try to call from the database I get a EntityCommandExecutionException with a InnerException message of Invalid object name 'dbo.ForumSections'.
Here is the code used to call the database.
First setting up the database context:
public class MainDbContext : DbContext
{
public MainDbContext()
: base("name=DefaultConnection")
{
}
public DbSet<ForumSection> ForumSection { get; set; }
}
Then calling it:
var db = new MainDbContext();
db.ForumSection.Find(0); //Error happens on this line
The row with index of 0 is appropriately filled out.
What are my options for resolving this?
By default, EntityFramework tries to look for the table pluralizing the name of the class. That's why it is adding the 's' to the ForumSection.
You can disable that option. This will help you solve your problem:
Why does EF 5.x use plural name for table?
I have a database issue. well im not sure exactly what to do. Im using the codefirst approach in an mvc5 project.
Currently my primary keys for my tables are auto generated and they increase. eg. customerid - 0,1,2,3 etc.
I want to know, how do i add a prefix to this id, eg i want it to be Cust0 instead of just 0.
The question is what is the reason you want to have this kind of "key"? You can auto generate ID for your customer and create your own property:
public class Customer
{
public const string CustomerPrefix = "Cust";
[Key]
public int Id {get; set;}
[NotMapped]
public string CustomerId
{
get { return string.Concat(CustomerPrefix, Id)}
}
}
Btw.: It is really bad practice to have PK as a string (because of performance)
I'm trying to create an app for Windows Phone 8 that displays data in a LongListSelector that's populated from a SQL CE database that's shipped with the app. I think I have the opening and reading from the database functions down, but I can't correctly use LINQ to SQL to group the data for the LLS.
I've got a database class with a table and corresponding columns. I'm using a helper class "KeyedList" to add a public name for the data from msdn sample code:
public class KeyedList<TKey, TItem> : List<TItem>
{
public TKey Key { protected set; get; }
public KeyedList(TKey key, IEnumerable<TItem> items)
: base(items)
{
Key = key;
}
public KeyedList(IGrouping<TKey, TItem> grouping)
: base(grouping)
{
Key = grouping.Key;
}
}
Then I've got my database context:
dB = new DataContext(DataContext.DBConnectionString);
Finally, here's the LINQ to SQL I'm trying to use:
var items =
from item in dB.TableName
orderby dB.ID
group item by dB.Generation into generation
select new <KeyedList<string,Item>(generation);
var allItems = new List<KeyedList<string, Item>>(items)
I've pretty much taken this code from the sample, but I can't get the grouping and ordering to work when creating allItems for binding to the LongListSelector. I keep getting invalid arguments error.
I'm very new at VB programming and appreciate all the help!
I found the issue. When creating the new Keyed list make sure to use the correct key type and item type. The key type will be the type of the data used by group by, and the item type is your DataContext. So in my case db.Generation is a string and the DataContext type is of type Item.
I want to implement a function that accepts a DbSet (non-generic), a string, and object, and returns DbSet. something like the following pseudu:
public static DbSet Any(DbSet set, string propertyName, objectParameter)
{
var tableName = set.TableName;
var columnName = set.GetColumnNameForProperty(propertyName);
var query = string.Format("SELECT TOP(1) {0} FROM {1} WHERE {0} = {2}",
columnName,
tableName,
objectParameter);
}
I think that SQL query is enough since I'll be able to execute it directly on the Database (context.Database.ExecuteSql).
What I want to do is get the table name from the given DbSet, then the column name in the database.
It is not possible from non generic DbSet but this problem can be easily solved by using:
public static IEnumerable<T> Any(DbSet<T> set, string property, objectParameter)
where T : class
{ ... }
Returning DbSet doesn't make sense because once you query data it is not DbSet anymore.
The bigger problem is getting table name from generic DbSet / ObjectSet because this information is not available from those classes. It is almost impossible to get it at all because it requires accessing non public members of items from MetadataWorkspace.
How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ:
select
type_id as id,
type_desc as description
from
dbo.equip_type_avt
order by
description
What do I replace the ????? in my .Select clause in my LINQ statement?
public IQueryable<equip_type_avt> GetGridEquipmentTypes(string sidx, string sord)
{
try
{
return
ulsDB.equip_type_avts
.Select(?????)
.OrderBy(sidx + " " + sord);
}
catch (Exception ex)
{
string strErr = ex.Message;
return null;
}
}
You can use an anonymous type:
table.Select(x => new
{
ID = x.type_id,
Description = x.type_desc
});
However, you can't access the properties of an anonymous type outside of the scope where it is declared (without reflection or other dirty hackery, anyway) so if you want to use the result outside of that function you just create a class and create an instance of it in the query using a type initializer:
public class Foobar
{
public int ID { get; set; }
public string Description { get; set; }
}
...
table.Select(x => new Foobar() // Note the difference here
{
ID = x.type_id,
Description = x.type_desc
});
Question though: if you want to name the columns differently, why don't you change it in the place where the column-property mapping is declared? In LINQ-to-SQL you can have the database column be named whatever you like but give the property the name "ID" or "Description".
I'm not sure i understand your question, how does sidx and sord relate to your query?
Isn't your problem rather that you have to end your query with OrderBy(...).ThenBy(...) instead of a combined OrderBy?
If you want to sort by a string in the easy way, download the Dynamic LINQ library.
However, that's 2000 lines of code, most of which are entirely redundant for just the purpose of sorting.
Doing it yourself shouldn't be too hard, but requires a fair bit of knowledge on expression trees. I can't really help you there though.
EDIT: I've added another answer, that hopefully answers your actual question :)