nullable

Is there an option to make Entity Framework revert empty strings to null?

南笙酒味 提交于 2019-12-03 13:11:47
问题 I am using an ADO.NET Entity-Framework ObjectContext to access my data store. I want the if values are set with empty strings, they should automatically become null. 回答1: If your using Entity Framework 4, you can use T4 templates to accomplish this. Just place this in the getter of each string property in your .tt template file, and it will replace empty strings with null and automatically trim strings. No need to use reflection. <#+ if (primitiveProperty.TypeUsage.ToString().Split('.').Last(

Argument order for '==' with Nullable<T>

和自甴很熟 提交于 2019-12-03 11:09:23
问题 The following two C# functions differ only in swapping the left/right order of arguments to the equals operator, == . (The type of IsInitialized is bool ). Using C# 7.1 and .NET 4.7 . static void A(ISupportInitialize x) { if ((x as ISupportInitializeNotification)?.IsInitialized == true) throw null; } static void B(ISupportInitialize x) { if (true == (x as ISupportInitializeNotification)?.IsInitialized) throw null; } But the IL code for the second one seems much more complex. For example, B is

Linq OrderBy breaks with navigation property being null

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:00:12
问题 Working with four tables. Users -> has basic user info including a userid and a departmentid (int) Groups -> basic group info including a groupid GroupsMembers -> table that has the relationship between a group and it's members, many to many relationship, so groupid and userid are the columns Departments -> basic department info including deptid I have a fk from the departmentid in the users table to the deparmtnet id in the departments table. FK from groups groupid to groupsmembers groupid

Why doesn't incrementing Nullable<int> throw an exception?

可紊 提交于 2019-12-03 10:20:58
问题 Could you please explain, why does Console.WriteLine write empty line ( Console.WriteLine(null) give me compilation error) and why there isn't NullReferenceException (even a+=1 shouldn't raise it)? int? a = null; a++; // Why there is not NullReferenceException? Console.WriteLine(a); // Empty line 回答1: You're observing the effects of a lifted operator . From section 7.3.7 of the C# 5 specification: Lifted operators permit predefined and user-defined operators that operate on non-nullable value

Nullable embedded value object with not nullable fields

旧街凉风 提交于 2019-12-03 09:32:06
I created an entity "Person" in Doctrine2, and I added to it an Adress entity, which is a value object (embeddable). I want to allow a Person creation, without an Address, so I tag my embedded as "nullable = true". But on the other hand, my Address entity, if it exists, SHOULD contains at least some information (like city, zip code, etc...). So it has "nullable = false" attributes. Address: type: embeddable fields: [...] city: type: string length: 255 nullable: false Person: type: entity table: null embedded: address: class: Address nullable: true It seems that the "nullable = true" of the

Is there a way to use the default value on a non-optional parameter when null is passed?

拜拜、爱过 提交于 2019-12-03 09:26:40
For example, if I have the following data class: data class Data( val name: String = "", val number: Long = 0 ) And functions that can return null : fun newName(): String? {} fun newNumber(): Long? {} I know I can use the following to use the value of the functions if they are not null : val newName = newName() val newNumber = newNumber() val data = Data( if (newName != null) newName else "", if (newNumber != null) newNumber else 0 ) But is there a way to just use the default value specified in the constructor of the Data class when the values are null ? I could not find anything in the

Best way to represent Nullable member in C++? [duplicate]

女生的网名这么多〃 提交于 2019-12-03 09:24:18
Possible Duplicate: Nullable values in C++ What is the best way to represent nullable member in C++? In C#, we can use Nullable<T> type. Such a data type is very much needed as not everything can have meaningful value. It is so important data type that @Jon Skeet has spent one entire chapter, spanned over 27 pages, describing only Nullable<T> in his outstanding book C# in Depth . One simple example can be a Person class 1 , defined as: struct Person { std::string Name; DateTime Birth; DateTime Death; //... }; As a person always have birthdate, so the Birth member of the above class will always

Trying to understand ?. (null-conditional) operator in C#

北城余情 提交于 2019-12-03 09:17:11
I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { System.Console.WriteLine("Contains elements"); } A a = null; if (a?.B) { System.Console.WriteLine("Is initialized"); } } } The line if (list?.Count > 0) compiles perfectly which means that if list is null , the expression Count > 0 becomes false by default. However, the line if (a?.B) throws a compiler error saying I can't implicitly convert bool? to bool . Why is one different from the other? Heinzi list?.Count > 0 : Here you compare

What is the justification for this Nullable<T> behavior with implicit conversion operators

百般思念 提交于 2019-12-03 08:15:12
问题 I encountered some interesting behavior in the interaction between Nullable and implicit conversions. I found that providing an implicit conversion for a reference type from a value type it permits the Nullable type to be passed to a function requiring the reference type when I instead expect a compilation error. The below code demonstrates this: static void Main(string[] args) { PrintCatAge(new Cat(13)); PrintCatAge(12); int? cat = null; PrintCatAge(cat); } private static void PrintCatAge

How to exclude null properties when using XmlSerializer

断了今生、忘了曾经 提交于 2019-12-03 08:14:15
问题 I'm serializing a class like this public MyClass { public int? a { get; set; } public int? b { get; set; } public int? c { get; set; } } All of the types are nullable because I want minimal data stored when serializing an object of this type. However, when it is serialized with only "a" populated, I get the following xml <MyClass ...> <a>3</a> <b xsi:nil="true" /> <c xsi:nil="true" /> </MyClass> How do I set this up to only get xml for the non null properties? The desired output would be